Skip to content

Instantly share code, notes, and snippets.

@jacobdanovitch
Last active December 15, 2017 00:39
Show Gist options
  • Save jacobdanovitch/c0a8c6e15908674c697b831962f7f6ee to your computer and use it in GitHub Desktop.
Save jacobdanovitch/c0a8c6e15908674c697b831962f7f6ee to your computer and use it in GitHub Desktop.
Quiz 2 Answers
'''
QUESTION 1
'''
def question1(number):
if number-int(number)==0:
return number
return number // 1 + 1
'''
We first use an if statement to check if the number is a whole number that doesn't need to be rounded.
i.e., 3.0 would not need to be rounded. 3.0 - int(3.0) == 0, so we simply return 3.0 and exit.
Otherwise, we round the number down using floor division ( // 1) and then add one, returning the ceiling.
Here are some test cases:
'''
#print(question1(3.4))
#print(question1(2.9))
#print(question1(3))
'''
QUESTION 2
'''
def question2(chars):
new_list = []
i = len(chars)-1
while i >= 0:
if ord(chars[i]) in range(ord('A'), ord('Z')+1):
new_list.append(ord(chars[i]))
i -= 1
return new_list
'''
Here, we begin by initializing a blank list to output.
Then, we create a loop counter set as the last index of the list of characters.
- So, in the example, the index would begin at chars[9], which contains 'D'.
We then use a while loop that will run while our counter is >= 0.
- Alternatively, the loop will run while there are still characters we haven't looked at.
Doing this iterates through the list in reverse.
We then check if the character is a capital letter.
- The ord() function checks the ASCII value of a character.
- If the ASCII value of chars[i] is in range(ord('A'), ord('Z')+1)...
- it means that it's in the range(65, 90)...
- which is the range of ASCII values for capital letters!
If it's a capital letter, we append the ASCII value to our list.
We then decrement i to continue moving backwards through the list.
Here are some test cases:
'''
#print(question2(['H', 'e', 'l', 'L', 'o', 'W', 'o', 'r', 'l', 'D']))
#print(question2("Quiz TIME"))
'''
QUESTION 3
'''
x = 3 #current value of x: 3
def foo(): #We're defining the function, not calling it. This won't do anything right now.
x=7
print(x)
return x
def bar(): #We're defining the function, not calling it. This won't do anything right now.
x = 5
print(x)
def qux(): #We're defining the function, not calling it. This won't do anything right now.
global x
x = 3
print(x)
''' Remove these quotes to run q3
x = 9 #Current value of x: 9
print(x) #Print current value of x -> OUTPUT 9
qux() #Current value of x: 3 (globally) ... Print current value of x -> OUTPUT 3
print(x) #Print current value of x -> OUTPUT 3
x = foo() #Create new x variable with value 7... Print new x -> OUTPUT 7...
#... return new x to global x ... Current value of x: 7
print(x) #Print current value of x -> OUTPUT 7
bar() #Create new x variable with value 5... Print new x -> OUTPUT 5
print(x) #Print current value of x -> OUTPUT 7
'''
'''
As demonstrated above, the output will be:
9
3
3
7
7
5
7
'''
'''
QUESTION 4
'''
def question4(phrase):
word_list = phrase.split(" ")
output = []
for word in word_list:
for char in word:
if ord(char) in range(ord('A'),ord('Z')+1):
output.append(word)
break
new_word = ''
for char in output[-1]:
if char not in ['.', '?', '!']:
new_word += char
output[-1] = new_word
return output
'''
Note: Careful with this question! It asks for words with ANY capitals; not just words starting with them.
We first split this list of words with the space between each word.
- This will give us a list: ['This', 'is', 'the'...]
-Note that the last word in the list will be 'quiz.', which has a period in it.
We create an empty list for our output.
We loop through each character of each word. This is essentially looping through every character of the input.
If any character within any word is a capital letter, add the word to the output and stop searching the word.
Now we deal with punctuation. We know that there will only be punctuation in the final word.
We create a blank string to hold our new word without punctuation.
We loop through the characters, adding them to our new word if they aren't punctuation.
We then set the last word of our output list to be this new, cleaned word, and return it.
Here are some test cases:
'''
#print(question4("This is the First question on your Second quiz."))
#print(question4("sTrInG pRoCeSsInG iS fun"))
'''
QUESTION 5
'''
from random import randint
def question5():
x = str(input("Please enter a comma separated list of integers.")).split(",")
temp = []
for num in x:
if int(num) % 2 == 0:
temp.append(int(num))
x = temp
if x == []:
print('Please provide a list containing at least one even number.')
return None
y = int(input("Choose how many rows you'd like. "))
z = int(input("Choose how many columns you'd like. "))
listception = []
for i in range(y):
row = []
for j in range(z):
row.append(x[randint(0,len(x)-1)])
listception.append(row)
return listception
'''
Remember to import the randint function!
We'll start off by taking a list of comma-separated integers from the user, defined as x (as specified).
We will then split x by these commas, returning a list.
- Note that this will return the comma separated numbers as STRINGS:
- i.e. ['1', '2', '3', ...]
We'll create a temporary list to clean up x.
We iterate through x, testing if int(num) is even using the modulo operator (%).
- Note that you must use int(nm), as currently, everything in x is a string.
- The modulo operator returns remainder. If num % 2 == 0, it has no remainder when divided by 2.
- If a number has no remainder when divided by 2, it's even.
We append every even number to our temporary list, ignoring the odd.
- Note that we must again make sure to append int(num), or num will still be a string.
We then update x to be our temporary list, which now only holds even integers from our original list.
- i.e., [2, 4, 6]
Now we have to make sure that the user even gave us an even integer. If they didn't, x will be empty.
- i.e., '1,99,1,1' is completely odd, so each number wil be ignored.
- Therefore, x will be an empty list like [ ].
If the user has not provided an even integer, we will notify them as such, and return None (null, if you prefer),
which will terminate the function.
We will then ask the user how many rows and columns they'd like in their 2D array, storing them in
y and z as specified. Remember to wrap your input() in an int()!
We'll then construct our 2D array using a nested loop, iterating through y rows of z columns.
We'll create an empty row [] list to hold our yth row.
Then, we'll add z columns of randomly chosen numbers from our list x to our row.
- We choose numbers rom our list x by choosing the INDEX randomly.
Now, we append the row to y.
- Note that the row [] list will be recreated as an empty list each iteration.
We now have our 2D array and return it as such.
Test cases:
Try...
- '1,2,3,4,5,6'
- '1,99,1,1'
- '10,20,30'
'''
#print(question5())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment