Python | addition of pattern 1 22 333 4444 55555 ....?
You generate a sequence of elements of the form str(i)*i
, which you cast into an integer, then sum
that sequence.
def create_sequence(n):
return sum(int(str(i)*i) for i in range(1, n+1))
create_sequence(int(input('Enter the number till which you want the sequence: ')))
I need to code a 1 22 333 4444 pattern in python with while loops
This is the idiomatic way to do it in Python, using the fact that a string can be "multiplied":
def EXwhile6 ():
a = input("Write how many lines you want to print: ")
a = int(a)
K = 1
while K <= a:
print(str(K) * K)
K += 1
Now, if you want to code it by hand you'd need a nested loop; the above is equivalent to:
def EXwhile6 ():
a = input("Write how many lines you want to print: ")
a = int(a)
K = 1
while K <= a:
J = 1
while J <= K:
print(K, end="")
J += 1
print()
K += 1
cannot create triangle in shell script with for loop
echo $i
prints the value behind the i
variable and a newline. You want to print a newline after all the numbers. Use -n
switch.
num=4 ##Assume num is given by user##
for ((i = 1; i <= num; i++))
do
for ((j = 1; j <= i; j++))
do
echo -n "$i "
done
echo
echo
done
Or printf:
num=4 ##Assume num is given by user##
for ((i = 1; i <= num; i++))
do
for ((j = 1; j <= i; j++))
do
printf "%s " "$i"
done
printf "\n\n"
done
- Remember to qoute your variables (
echo "$i"
notecho $i
) - No need to use
$num
inside(( .. ))
. All names are expanded automagically.
@edit
On revisiting the question I came up with the following oneliner with the same functionality:
num=4
seq 1 "$num" | xargs -n1 seq -s ' ' 1
seq 1 "$num"
generates numbers 1 2 .... $num separated by newlines- Then for each number
xargs
runsseq -s ' ' 1 <number>
generating 1 .. number for each number separated by spaces - You can insert double newlines with
| sed 's/$/\n/'
if needed.
Concatenate integers in a list and print it, without using a python for loop
According to this answer it is called "sum of a geometric progression"; after googling a bit more, this kind of number/sequence seems to be known as one kind of Smarandache Sequence.
You can try this:
for i in range(1, int(input())):
print(int(i * ((10**i) - 1) / 9))
It is a confusing one liner, but it seems to work.
How to print a number sequence figure using for loops
I think you have over thought this. I would simplify things and use a different algorithm. Initiate a String
and mutate it with one loop. Like,
String s = "122333444455555";
for (int i = 0; i < 5; i++) {
System.out.println(s);
s = s.substring(i + 1);
}
Outputs (as requested)
122333444455555
22333444455555
333444455555
444455555
55555
exactly n times - group
You cannot. Groups always correspond to capturing groups in the regular expression. That is, if you have one capturing group, there cannot be more than one group in the match. It is irrelevant how often a portion (even a capturing group) is repeated during the match. The expression itself defines how many groups the final match can have.
Related Topics
How to Determine the Highest and Lowest Value Using Do While Loops
Difference Between Function Template and Template Function
Why Is "Using Namespace Std;" Considered Bad Practice
Is It a Good Idea to Typedef Pointers
How to Get the List of Files in a Directory Using C or C++
Pointer to Class Data Member "::*"
What Is "Rvalue Reference For *This"
Opencv C++/Obj-C: Detecting a Sheet of Paper/Square Detection
What Is the Bit Size of Long on 64-Bit Windows
Singleton: How Should It Be Used
How to Have All the Inputs on the Same Line C++
C++: How to Iterate Over Each Char in a String
Why Can Templates Only Be Implemented in the Header File
What Uses Are There For "Placement New"
Can Num++ Be Atomic For 'Int Num'
Erasing Elements from a Vector
How to Remove Code Duplication Between Similar Const and Non-Const Member Functions