#Quiz09 – Distance

--Originally published at Hello World

Here are the instructions:

Write a function that receives four parameters: x1, y1, x2, y2 which are all floating point values.The function is called distance and returns (float) the distance between x1,y1 and x2,y2 on the cartesian coordinate plane.


#Quiz08

--Originally published at Hello World

Here are the instructions:

Write a function that calculates returns the “nth” Fibonacci number where we define a function over the Fibonacci numbers mapping the naturals (starting with zero) to the Fibonacci series. So fibonacci(0) returns 0, fibonacci(1) returns 1, fibonacci(2) returns 1 and so on. Note that we are using the modern definition where the sequence starts with zero. You should try to implement this with two solutions: one with a loop and one with recursion. Which do you think is “better”, which looks more “elegant”, which is more “efficient”?


Exercises 11 to 15

--Originally published at Hello World

  • Define a function generate_n_chars() that takes an integer n and a character c and returns a string, n characters long, consisting only of c:s. For example, generate_n_chars(5,"x") should return the string "xxxxx". (Python is unusual in that you can actually write an expression 5 * "x" that will evaluate to "xxxxx". For the sake of the exercise you should ignore that the problem can be solved in this manner.)Screen Shot 2017-02-27 at 10.49.54 am.png
  • Define a procedure histogram() that takes a list of integers and prints a histogram to the screen. For example, histogram([4, 9, 7]) should print the following:
    ****
    *********
    *******
  • The function max() from exercise 1) and the function max_of_three() from exercise 2) will only work for two and three numbers, respectively. But suppose we have a much larger number of numbers, or suppose we cannot tell in advance how many they are? Write a function max_in_list() that takes a list of numbers and returns the largest one.
  • Write a program that maps a list of words into a list of integers representing the lengths of the correponding words.
  • Write a function find_longest_word() that takes a list of words and returns the length of the longest one.

Computing / Music

--Originally published at Hello World

Computing is about

ABSTRACTION & ENCAPSULATION

Music and programming are related. Programming is art.

It is about abstraction, representing things in something basic while removing all the extra stuff

Encapsulation is the reverse of abstraction. In general, encapsulation is the inclusion of one thing within another thing so that the included thing is not apparent.


#WSQ07 – Lists

--Originally published at Hello World

Here are the instructions:

Create a program that asks the user for 10 numbers  (floating point). Store those numbers in a list. Show to the user the total, average and standard deviation of those numbers.

 

It was easy, just needed to import the statistics module to perform the operations. The only trick was not to use int(input()) but float(input()) as the user was asked to enter floating numbers.


#Quiz06

--Originally published at Hello World

  • Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) should return 24.

 

Screen Shot 2017-02-20 at 7.46.17 pm.png

For this exercise, we start by setting my_sum to 0 as inside the for loop the update occurs. my_sum is reassigned a new value which is my_sum + each element in the list.

  • Define a function reverse() that computes the reversal of a string. For example, reverse("I am testing") should return the string "gnitset ma I".

Screen Shot 2017-02-20 at 8.00.51 pm.png

Thanks to [::-1], we can have the sentence written by the user reversed!

  • Define a function is_palindrome() that recognises palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar") should return True.

 

  • Write a function is_member() that takes a value (i.e. a number, string, etc) x and a list of values a, and returns True if x is a member of a, False otherwise. (Note that this is exactly what the in operator does, but for the sake of the exercise you should pretend Python did not have this operator.)

 

  • Define a function overlapping() that takes two lists and returns True if they have at least one member in common, False otherwise. You may use your is_member() function, or the in operator, but for the sake of the exercise, you should (also) write it using two nested for-loops.

WSQ06 – Fatorial calculator

--Originally published at Hello World

Here are the instructions:

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n). For the Python group, resist the urge to call math.factorial(n). Yes that would solve the problem but what would we do if there was no math.factorial() and we had no internet to find someone’s solution? There are two basic approaches: a loop with an accumulator of the multiplication and a recursive solution. Choose one and implement that. Once that is done, try the other way. If you used a while loop for the solution with a loop, try structuring this with a for loop (or vice-versa).


WSQ05 On To Functions

--Originally published at Hello World

Here are the instructions:

You will go back and do WSQ01 – Fun with Numbers again.But this time, write a function for each calculation. Each function should define two parameters (in this example of type int) and return the correct value as an integer as well.You main program needs to ask the user for the input and then call each function to calculate the answer for each of the parts


WSQ03 – Pick a Number

--Originally published at Hello World

Here are the instructions:

Write a program that picks a random integer in the range of 1 to 100.There are different ways to make that happen, you choose which one works best for you.It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.You might want to check that your program doesn’t always use the same random number is chosen and you should also split your problem solving into parts. Perhaps only generate the random number and print that as a first step.

 

  1. I found how to ask Python for a random integer using https://www.tutorialspoint.com/python/number_randrange.htm. You only need to import random and call for random.randrange(first number, last number)
  2. I only printed the random number r to make sure I know what it was and that the program was functional.
  3. x is the number the user is guessing each time
  4. trial is the number of trials: every time the user guesses a wrong number (higher or lower than the random number generated by Python), a trial is counted.
  5. while was the most important input to my program! Indeed, I read this article https://www.tutorialspoint.com/python/python_while_loop.htm and everything was clearer! While repeat the block forever. The way to stop it is the print!

 


#Quiz05

--Originally published at Hello World

  • Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Python. (It is true that Python has the max() function built in, but writing it yourself is nevertheless a good exercise.)

Screen Shot 2017-02-09 at 6.36.38 pm.png

For this, you only need to define the function called largest_number, and use the build-in function max() in python. Then ask the user for two numbers, and the function gives you the largest of the two numbers.

 

  • Define a function max_of_three() that takes three numbers as arguments and returns the largest of them.

Screen Shot 2017-02-09 at 6.43.47 pm.png

Same thing as above, but you just need to add a third variable!

 

  • Define a function that computes the length of a given list or string. (It is true that Python has the len() function built in, but writing it yourself is nevertheless a good exercise.)

Screen Shot 2017-02-09 at 6.46.03 pm.png

Here you need to define a function called length_of_string, then return the Python build-in function len(). Ask the user for a sentence (and do not forget to convert the sentence into a string). The functions returns you the number of characters in your sentence.

 

 

  • Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.

Screen Shot 2017-02-09 at 6.49.24 pm.png

You need to ask the user for a letter. Then use the conditional execution if .. in ( “a”, “e”, “i”, “o”, “u”), in order to detect a vowel. Else will detect other characters which are not vowels. I found this website helpful for this question: http://stackoverflow.com/questions/20226110/detecting-vowels-vs-consonants-in-python

 

  • Write a function translate() that will translate a text into “rövarspråket” (Swedish for “robber’s language”). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun")should return the
    Screen Shot 2017-02-09 at 6.59.23 pm.png
    Continue reading "#Quiz05"