AA Practice Problems

¡Supera tus tareas y exámenes ahora con Quizwiz!

Write a method that takes in a string and returns the number of letters that appear more than once in the string. You may assume the string contains only lowercase letters. Count the number of letters that repeat, not the number of times they repeat in the string.

1. Create a hash called 'count' to store each unique letter and its count. 2. Compare each letter against the rest of the letters in the string by incrementing idx1 and idx2. string[0] == string[1] a == a? string[0] == string[2] a == a? string[1] == string[2] a == a? 3. If it repeats, count +=1 and store it in the hash. count[string[0]] += 1 => count = {'a' => 3} 4. Count the number of key-value pairs in the count hash by using count.length and return that value.

Write a method that takes in a string of lowercase letters (no uppercase letters, no repeats). Consider the *substrings* of the string: consecutive sequences of letters contained inside the string. Find the longest such string of letters that is a palindrome. Note that the entire string may itself be a palindrome. You may want to use Array's 'slice(start_index, length)' method, which returns a substring of length 'length' starting at index 'start_index':

1. Write a method to check if string is a palindrome. 2. Build a list of potential substrings by slicing them out at different indices and lengths. Example: indices: 0 1 2 3 4 5 6 7 8 string: a b c b d e f f e => e f f e idx = 0: a, ab, abc, abcb, abcbe, abcbdef, abcbdeff, abcbdeffe idx = 1: b, bc, bcb, bcbd, bcbde, bcbdef, bcbdeff, bcbdeffe idx = 2: c, cb, cbd, cbdef, cbdeff, cbdeffe idx = 3: b, bd, bdef, bdeff, cbdeffe idx = 4: d, de, def, deff, bdeffe idx = 5: e, ef, eff, effe idx = 6: f, ff, ffe idx = 7: f, fe idx = 8: e 3. Use previous 'palindrome?' method to check if each substring is a palindrome. 4. If it is a palindrome, compare its length to the previous palindrome and if it's greater, replace it as the longest palindrome.

Write a method that takes an integer 'n' in; it should return n*(n-1)*(n-2)*...*2*1. Assume n >= 0. As a special case, factorial(0) == 1.

factorial = the product of all positive integers less than or equal to n.

Write a method that takes in an integer (greater than one) and returns true if it is prime; otherwise return false.

prime number = whole numbers greater than 1 that can only be divided by 1 and itself without remainder. Examples: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29 Has remainder = prime (number % i != 0) Has no remainder = not prime (number % i == 0)


Conjuntos de estudio relacionados

Retirement and other insurance concepts

View Set

directional anatomy practice in sentences

View Set

Principles of Financial Accounting

View Set

Examples of Assets, Liabilities and Equity

View Set