Skip to content

Instantly share code, notes, and snippets.

@theniceboy
Last active July 9, 2022 05:00
Show Gist options
  • Save theniceboy/368b1f718c881ca746744f30d99a564b to your computer and use it in GitHub Desktop.
Save theniceboy/368b1f718c881ca746744f30d99a564b to your computer and use it in GitHub Desktop.
Practice problem #1

Printing triangles

Part I

Write the function repeatString that takes a String and an integer, and returns a String built by repeating the given String given number of times.

For example, one should be able to call

print(repeatString("*", 10));

and get

**********

as the output.

Hint

You can add to Strings together by:

String a = "Hello, ";
String b = "Alina";
print(a + b); // It'll print out "Hello, Alina"

Part II

Use the printString function you wrote along with a for loop to print out this 6x6 triangle:

*
**
***
****
*****
******

Part III (Optional)

Use the printString function you wrote along with a for loop to print out this triangle:

*******
 *****
  ***
   *
@ahhhchoo
Copy link

ahhhchoo commented Jul 5, 2022

Screen Shot 2022-07-05 at 3 46 46 PM

part1

@theniceboy
Copy link
Author

Nice! @ahhhchoo can you do it with a for loop as well?

@ahhhchoo
Copy link

ahhhchoo commented Jul 5, 2022

Screen Shot 2022-07-05 at 3 55 47 PM

i broke my dart

@theniceboy
Copy link
Author

Screen Shot 2022-07-05 at 3 55 47 PM

i broke my dart

Lol

btw by for loop I mean within the function

@ahhhchoo
Copy link

ahhhchoo commented Jul 5, 2022

my dart keeps crashing, i think because i wrote the loop wrong.

@ahhhchoo
Copy link

ahhhchoo commented Jul 5, 2022

Screen Shot 2022-07-05 at 4 02 44 PM

@theniceboy
Copy link
Author

Yeah because the “star” variable never changes. You can increment a variable by writhing “star = star + 1”

@ahhhchoo
Copy link

ahhhchoo commented Jul 6, 2022

Screen Shot 2022-07-06 at 4 47 26 PM

@theniceboy
Copy link
Author

Screen Shot 2022-07-06 at 4 47 26 PM

Very nice

@ahhhchoo
Copy link

ahhhchoo commented Jul 7, 2022

Im confused, why isnt star + 1 equivalent to star ++ ? Also i didnt know what you meant by your comment "Yeah because the “star” variable never changes. You can increment a variable by writhing “star = star + 1” So i did my own workaround and apparently it worked when I used ++

@theniceboy
Copy link
Author

Oh yeah sorry star++ is equivalent to star = star + 1

@theniceboy
Copy link
Author

star + 1 by itself will not change star, star = blahblah will

@ahhhchoo
Copy link

ahhhchoo commented Jul 8, 2022

Ahh i see, thank you so much. I was so confused why it wasn't working.

@theniceboy
Copy link
Author

cool! Np! Do you think you can tackle part 3?

@ahhhchoo
Copy link

ahhhchoo commented Jul 9, 2022

How can i use 'star' in my repeatString function?

@theniceboy
Copy link
Author

You don’t. You want to call repeatString function multiple times every loop cycle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment