/programming-exercises

Michael Herman's stash of programming exercises

Primary LanguageHTML

Problems

Solved with brute force

  1. Generate two random integers and print their mean, variance, and standard deviation.
  2. Calculate the mean, variance, and standard deviation for a list/array containing 10 random integers.
  3. Generate twenty random integers from 0 to 100 and print the largest and smallest integers.
  4. Generate a single random number, from 0 to 100, then output all the even integers from 0 to the random number.
  5. Generate a single random number, from 0 to 100. If the number is greater than 40 then output all the odd integers from 40 to the random number. If the number is less than 40, output all the odd integers from 0 to 40.
  6. Generate a single random number, from 0 to 100. If the number is greater than 40 then output all the odd integers from that number down to 40. If the number is less than 40, output all the odd integers from that number down to 0.
  7. Discount Percentage: Write a function that takes a dollar amount and a discount percentage and returns the total discount amount. Return a warning if the discount amount is greater than 100 or less that 0 percent. Test the results.
  8. Class it up. Wrap a class around the last function. Instantiate the class and call the function. Then use unittests and rspec to test.
  9. Generate n random integers from 0 to 100, then output them in ascending order.
  10. Generate a random number from 1000 to 9999 and output the sum of its digits. For example, if the number is 1049, then the output should be 14.
  11. Generate a random number from 1000 to 9999 and output the sum of its digits, repeatedly, dropping the left-most digit after each iteration, until there is only one digit only. For example, if the number is 1049, then the output should be 14, 13, 13, 9.
  12. Generate two random integers from 0 to 100 and output the smallest number such that each shares a least common denominator (aside for 1). For example, 18 and 6 should return 6, while 9 and 4 should return False. Return False if the two numbers are equal.
  13. Write a function that takes two random integers as arguments and returns the least common multiple.
  14. Write a program that calculates and shows the value of (x to the nth power). Let both integers fall within the range of 2 through 20.
  15. Write a function that returns ten lines. On the first line there is one '#' character. On the second there are two. And so on. This will create a nice triangle.
  16. Write a function that takes two random, 10-character strings as arguments, which returns True when the first string starts with the first two characters in the second string - and false otherwise.
  17. Write a function that takes a paragraph as an argument and returns an array that contains each string as an element.
  18. Create a simple webpage that includes three tags, an <h1>, <h2> and <p>, then using jQuery, create an alert that outputs the tag name when you click on the tag.
  19. Write a function that takes an object/hash/dictionary of people (keys) and their subseuqent ages as its argument, and then it returns the name of the oldest living person.
  20. Write two functions. Both take a base and an exponent as arguments and return the value of base^exponent. Use recursion in the first function and a loop in the second. Benchmark the results.
  21. Reverse: Write a function that takes a string as an argument and returns the string in reversed order. Test if the string is the same reversed (Palindrome).
  22. Factorial: Write a function that takes a random number as an argument and then returns the factorial of that given number.
  23. Longest Word: Write a function that takes a string and returns the longest word in the string.
  24. Longest Word 2: Refactor the "Longest Word" function so that it ignores punctuation.
  25. Time Difference: Write a function that takes two times in hours and minutes and returns the difference.
  26. Time Convert: Write a function that takes an integer then returns the number in hours and minutes. For example, 63 should return 1:3.
  27. Using JavaScript, create a function that handles errors. The function takes two integers as inputs and returns the result of dividing them. If the denominator is 0, throw an error.
  28. Spiral: Write a function called "spiral" that takes an NxM array as input and returns a "spiral" path through the array, starting in the upper-left corner and moving clockwise. For example: [[1,2,3],[8,9,4],[7,6,5]] => [1,2,3,4,5,6,7,8,9]
  29. Password Generator: Write a function that takes an integer as an input and returns a random password - with upper and lowercase letters, integers, and characters - the length of the integer.
  30. Using jQuery and CSS, create new .js and .css files (prob30.css and prob30.js, respectively) so that when a user clicks on a button, the background color of the page will change. Use the HTML code in the corresponding folder. You do not have to edit the file.
  31. Mad Libs: Using HTML, CSS, and jQuery, create a mad libs game. Use an HTML form to take user input. Display the results however you'd like. Get creative.
  32. Create a simple to-do list using jQuery. Make sure the user can remove the items.
  33. Create an app that pulls in your Github repos using AJAX. PUSH it to Heroku (using static files). Document the PUSH process. http://git-ajax.herokuapp.com/
  34. Write a function that takes a single string as the input then return the number of vowels the string contains. Do not count "y" as a vowel.
  35. Create a function called "ex_oh" that takes a string of "x"s and "o"s as inputs. Return True if the number of "x"s and "o"s is even - and False otherwise.
  36. Write a function that takes an array of integers and returns the number of users in it. Then refactor this to use a helper function to first test whether an integer is zero or not that's called from the original function.
  37. Write a function that takes the following arguments-
    • % The Book of Programming
    • %% The Two Aspects
    • Below the surface of the machine, the program moves. Without effort, it expands and contracts. In great harmony, electrons scatter and regroup. The forms on the monitor are but ripples on the water. The essence stays invisibly below.

And then checks the type (HTML tag) of the argument (H1, HX, or p). H1 has one "%", HX has two "%", and a p has none. Return each argument without the "&" characters as well as the type.

  1. Build a function, splitParagraph(), which, takes a paragraph string as an input, and returns an array of paragraph fragments. (do not use any punctuation)
  2. Guessing Game: Create a basic website that asks the user to input an integer from 1 to 100. When the user inputs a number, it should tell the user if the number they entered is too high, too low, or the exact number. When the game is over - e.g., when the user guesses correctly - display a "Start Over" button allowing the user to play again! Also, add a "Give Up" button, which displays the number and starts the game over. You should only have three files - index.html, main.js, and main.css. Use either jQuery or Angular for interacting with the DOM.
  3. One Missing: Write a function that takes an array of integers between 1 and 10, with one missing integer, as an argument and returns the missing integer. Test this with Mocha and Chai.
  4. Find Odd Occurring Element: Given an integer array, one element occurs odd number of times and all others have even occurrences. Find the element with odd occurrences.
  5. Find Next Higher Number With Same Digits: Write a function that takes an integer and returns the next higher number that uses only the digits in that number. For example - 1345 => 1354. Hint: Write tests first!
  6. Longest Palindrome: Write a function that takes a string as an argument and finds the longest palindrome, including whitespace. So, given the string "I am a dazzled racecar driver", the functon should return "d racecar d".
  7. Byte String: Write a function that takes an integer as an argument and converts it to a string that represents a rounded size in bytes. Examples:
  • 10221 => "9.98 KB"
  • 1022932324 => "975.54 MB"