Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active April 17, 2024 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MisterBrash/df850424a11413b6660ac0b6b930852a to your computer and use it in GitHub Desktop.
Save MisterBrash/df850424a11413b6660ac0b6b930852a to your computer and use it in GitHub Desktop.
ICS3U More For-Loop Practice

More For Loop Practice

Are you fully caught up on while, do while and for loops? Looking for more practice?
You can use the same code file that you used last class or make a new one - up to you.

Try these challenges out

  • printLine(char, width) that prints a single line of char's of length width. You will use a for-loop to accomplish this. For Example:

    > printLine("H", 5)
    HHHHH
    > printLine("🤯", 10)
    🤯🤯🤯🤯🤯🤯🤯🤯🤯🤯
    > printLine("👻", 3)
    👻👻👻

  • printRect(char, height, width) that prints a rectangle of char's with the given height and width. You will use a for-loop to do this and you should use your printLine() function from above to help. Example:

    > printRect("@", 4, 6)
    @@@@@@
    @@@@@@
    @@@@@@
    @@@@@@

  • Read this one carefully, especially the return format. The Fibonacci Sequence is a series of numbers. The first two numbers are 0 and 1. The next numbers are found by adding up the two numbers before.

    For example: 0, 1, 1, 2, 3, 5, 8, 13, 21. The next number in this series is 13+21 = 34.

    Write the function fib(n) that returns a String containing n values of the Fibonacci Sequence. Each value in the return String is separated by a comma and a space ", "

    For Example: fib(5) returns the string "0, 1, 1, 2, 3" meanwhile fib(11) returns the string "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55". Assume the parameter, n, is an integer value >= 1. You will use a for-loop to accomplish this.



Looking for a harder challenge?

  • count(char, str)
    Write the function count(char, str) - given the string str count how many times char is found in the string and return that number.
    For Example:

    > count("r", "Mr. Squirrel");
    3
    > count(".", "Mr. Squirrel");
    1
    > count("Z", "Mr. Squirrel");
    -1
    • If you never find the character, return -1.

Looking for a REALLY hard challenge?

  • squirrel_crypt(str)
    Mr. Squirrel has developed an encryption algorithm that he's pretty sure nobody has every used before. It's so good that it can encrypt and decrypt a message when you use it.
    • Each letter is replaced with its reflective partner in the alphabet (A becomes Z, B becomes Y, M becomes N, O becomes L, S becomes H, etc). Huge hint - M and N are the middle of the alphabet.
    • Each letter is changed from UPPERcase to lowercase and from lowercase to UPPERcase
    • Spaces " " are replaced with commas "," and commas are replaced with spaces
    • All other special characters are left alone
    • The string is reversed (Hello <> olleH)
    • The new string is returned!
      Examples:
      squirrel_crypt("Here's a simple test.");
      // returns:  .GHVG,VOKNRH,Z,H'VIVs
      
      squirrel_crypt(".GHVG,VOKNRH,Z,H'VIVs");
      // returns:  Here's a simple test.
      
      squirrel_crypt("The FBI agent's CSIS friend doesn't believe in UFO's!");
      // returns:  !H'luf,MR,VEVROVY,G'MHVLW,WMVRIU,hrhx,H'GMVTZ,ryu,VSg
      
      squirrel_crypt("!H'luf,MR,VEVROVY,G'MHVLW,WMVRIU,hrhx,H'GMVTZ,ryu,VSg");
      // returns:  The FBI agent's CSIS friend doesn't believe in UFO's!



🐿️

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