DEV Community

Discussion on: Daily Challenge #201 - Complete the Pattern

Collapse
 
vidit1999 profile image
Vidit Sarkar

I think there is no new-line at the end.
Here is C++ solution:

string pattern(int number){
    string pat = "";

    // there is no new-line at the end so loop end is number-1
    for(int i=1;i<=number-1;i++){
        for(int j=0;j<i;j++)
            pat += to_string(i); // add the string version of number
        pat += "\n"; // add a new-line after adding the numbers each time 
    }

    // add the case of number at the end with no new-line
    for(int i=0;i<number;i++)
        pat += to_string(number);

    return pat;
}
Collapse
 
vidit1999 profile image
Vidit Sarkar • Edited

Python one liner :

pattern = lambda number : '\n'.join([str(i)*i for i in range(1,int(number)+1)])