﻿ Writing a Sequence of Numbers Like: 1 22 333 4444 55555 - ITCodar

# Writing a Sequence of Numbers Like: 1 22 333 4444 55555

## 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    echodone``

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``
1. Remember to qoute your variables (`echo "\$i"` not `echo \$i`)
2. 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=4seq 1 "\$num" | xargs -n1 seq -s ' ' 1``
1. `seq 1 "\$num"` generates numbers 1 2 .... \$num separated by newlines
2. Then for each number `xargs` runs `seq -s ' ' 1 <number>` generating 1 .. number for each number separated by spaces
3. 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)

``1223334444555552233344445555533344445555544445555555555``

## 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.