Challpy

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

functions: and age.

1. Tenth Power The function should compute the age in dog years and

Write a function named tenth_power() that has one parameter named num. return the following string:

The function should return num raised to the 10th power. Test this function with your name and your age!

2. Square Root "{name}, you are {age} years old in dog years"

Write a function named square_root() that has one parameter named num. 5. All Operations

Use exponents (**) to return the square root of num. Create a function named lots_of_math(). This function

3. Win Percentage should have four parameters named a, b, c, and d. The

Create a function called win_percentage() that takes two parameters named wins and losses. function should print 3 lines and return 1 value.

This function should return out the total percentage of games won by a team based on these two numbers. First, print the sum of a and b.

4. Average Second, print d subtracted from c.

Write a function named average() that has two parameters named num1 and num2. Third, print the first number printed, multiplied by the

The function should return the average of these two numbers. second number printed.

5. Remainder Finally, return the third number printed mod a.

Write a function named remainder() that has two parameters named num1 and num2. control flow:

The function should return the remainder of twice num1 divided by half of num2. 1. Large Power

1. First Three Multiples Create a function named large_power() that takes two parameters named base and exponent.

Write a function named first_three_multiples() that has If base raised to the exponent is greater than 5000, return True, otherwise return False

one parameter named num. 2. Over Budget

This function should print the first three multiples of num. Create a function called over_budget that has five parameters named budget, food_bill, electricity_bill, internet_bill, and rent.

Then, it should return the third multiple. The function should return True if budget is less than the sum of the other four parameters — you’ve gone over budget! Return
False otherwise.
For example, first_three_multiples(7) should print 7, 14,
3. Twice As Large
and 21 on three different lines, and return 21.
Create a function named twice_as_large() that has two parameters named num1 and num2.
2. Tip
Return True if num1 is more than double num2. Return False otherwise.
Create a function called tip() that has two parameters
4. Divisible By Ten
named total and percentage.
Create a function called divisible_by_ten() that has one parameter named num.
This function should return the amount you should tip
The function should return True if num is divisible by 10, and False otherwise. Consider using modulo (%) to check for
given a total and the percentage you want to tip. divisibility.
3. Bond, James Bond

Write a function named introduction() that has two 5. Not Equal


parameters named first_name and last_name. Create a function named not_sum_to_ten() that has two parameters named num1 and num2.
The function should return the last_name, followed by a Return True if num1 and num2 do not sum to 10. Return False otherwise.
comma, a space, first_name another space, and finally challenge 2 :
ast_name. 1. In Range
4. Dog Years Create a function named in_range() that has three parameters named num, lower, and upper.
Some say that every one year of a human’s life is The function should return True if num is greater than or equal to lower and less than or equal to upper. Otherwise, return False.
equivalent to seven years of a dog’s life. Write a function 2. Same Name
named dog_years() that has two parameters named name
Create a function named same_name() that has two parameters named your_name and my_name. 2. Remove Middle

If our names are identical, return True. Otherwise, return False. Create a function named remove_middle which has three parameters named lst, start, and end.

3. Always False The function should return a list where all elements in lst with an index between start and end (inclusive) have been removed.

Create a function named always_false() that has one parameter named num. For example, the following code should return [4, 23, 42] because elements at indices 1, 2, and 3 have been removed:

Using an if statement, your variable num, and the operators >, and <, make it so your function will return False no matter what remove_middle([4, 8 , 15, 16, 23, 42], 1, 3)
number is stored in num.
3. More Frequent Item
An if statement that is always false is called a contradiction. You will rarely want to do this while programming, but it is
important to realize it is possible to do this. Create a function named more_frequent_item that has three parameters named lst, item1, and item2.

4. Movie Review Return either item1 or item2 depending on which item appears more often in lst.

Create a function named movie_review() that has one parameter named rating. If the two items appear the same number of times, return item1.

If rating is less than or equal to 5, return "Avoid at all costs!". If rating is between 5 and 9, return "This one was fun.". If rating is 4. Double Index
9 or above, return "Outstanding!" Create a function named double_index that has two parameters: a list named lst and a single number named index.
5. Max Number The function should return a new list where all elements are the same as in lst except for the element at index. The element at
Create a function called max_num() that has three parameters named num1, num2, and num3. index should be double the value of the element at index of the original lst.

The function should return the largest of these three numbers. If any of two numbers tie as the largest, you should return "It's a If index is not a valid index, the function should return the original list.
tie!". For example, the following code should return [1,2,6,4] because the element at index 2 has been doubled:
lists: double_index([1, 2, 3, 4], 2)
1. Append Sum After writing your function, un-comment the call to the function that we’ve provided for you to test your results.
Write a function named append_sum that has one parameter — a list named named lst. 5. Middle Item
The function should add the last two elements of lst together and append the result to lst. It should do this process three times and Create a function called middle_element that has one parameter named lst.
then return lst.

For example, if lst started as [1, 1, 2], the final result should be [1, 1, 2, 3, 5, 8].
If there are an odd number of elements in lst, the function should return the middle element. If there are an even number of
2. Larger List elements, the function should return the average of the middle two elements.
Write a function named larger_list that has two parameters named lst1 and lst2. loops:
The function should return the last element of the list that contains more elements. If both lists are the same size, then return the CODE CHALLENGE: LOOPS
last element of lst1.
-------------------------------------------------------
3. More Than N
Divisible by Ten
Create a function named more_than_n that has three parameters named lst, item, and n.
divisible_by_ten(nums)
The function should return True if item appears in the list more than n times. The function should return False otherwise.
Create a function named divisible_by_ten() that takes a list of numbers named nums as a parameter.
4. Append Size
Return the count of how many numbers in the list are divisible by 10.
Create a function called append_size that has one parameter named lst.
-------------------------------------------------------------------------
The function should append the size of lst (inclusive) to the end of lst. The function should then return this new list.
Greetings
For example, if lst was [23, 42, 108], the function should return [23, 42, 108, 3] because the size of lst was originally 3.
add_greetings(names)
5. Combine Sort
Create a function named add_greetings() which takes a list of strings named names as a parameter.
Write a function named combine_sort that has two parameters named lst1 and lst2.
In the function, create an empty list that will contain each greeting. Add the string "Hello, " in front of each name in names and
The function should combine these two lists into one new list and sort the result. Return the new sorted list. append the greeting to the list.
1. Every Three Numbers Return the new list containing the greetings.
Create a function called every_three_nums that has one parameter named start. --------------------------------------------------
The function should return a list of every third number between start and 100 (inclusive). For example, every_three_nums(91) Delete Starting Even Numbers
should return the list [91, 94, 97, 100]. If start is greater than 100, the function should return an empty list.
delete_starting_evens(lst)
Write a function called delete_starting_evens() that has a parameter named lst. Create variables named sum1 and sum2 and set them to be 0. Loop through each list separately and add to the appropriate
variable. After looping through each list, compare the two sums in an if statement and return the correct list.
The function should remove elements from the front of lst until the front of the list is not even. The function should then return lst.
-----------------------------------------
For example if lst started as [4, 8, 10, 11, 12, 15], then delete_starting_evens(lst) should return [11, 12, 15].
Over 9000
Make sure your function works even if every element in the list is even!
over_nine_thousand(lst)
Hint
Create a function named over_nine_thousand() that takes a list of numbers named lst as a parameter.
Use a while loop to check two things. First, check if the list has at least one element, using len(lst). Second, check to see if the first
element is odd using mod (%). If both of those are True, slice off the first element of the list using lst = lst[1:]. The function should sum the elements of the list until the sum is greater than 9000. When this happens, the function should return
the sum. If the sum of all of the elements is never greater than 9000, the function should return total sum of all the elements. If the
-------------------------------------------------- list is empty, the function should return 0.
Odd Indices For example, if lst was [8000, 900, 120, 5000], then the function should return 9020.
odd_indices(lst) Hint
Create a function named odd_indices() that has one parameter named lst. Create a variable named sum that begins at 0. Loop through all of the elements of lst and use a break when the sum is greater than
The function should create a new empty list and add every element from lst that has an odd index. The function should then return 9000. Return sum after the loop.
this new list. -------------------------------------
For example, odd_indices([4, 3, 7, 10, 11, -2]) should return the list [3, 10, -2]. Max Num
Hint max_num(nums)
There are a few ways to do this. range(1, len(lst), 2) will create a list of the indices you’re interested in. So you could loop through Create a function named max_num() that takes a list of numbers named nums as a parameter.
that list like this:
The function should return the largest number in nums
for index in range(1, len(lst), 2):
Hint
new_list.append(lst[index])
Create a variable called maximum to track the max number, and have it start as the first element in the list. Loop through all of the
Concept Review numbers in the list, and if a number is ever greater than the current max number, the max number should be re-set to that number.
--------------------------------------------------- ----------------------------------
Exponents Same Values
exponents(bases, powers) same_values(lst1, lst2)
Create a function named exponents() that takes two lists as parameters named bases and powers. Return a new list containing Write a function named same_values() that takes two lists of numbers of equal size as parameters.
every number in bases raised to every number in powers.
The function should return a list of the indices where the values were equal in lst1 and lst2.
For example, consider the following code.
For example, the following code should return [0, 2, 3]
exponents([2, 3, 4], [1, 2, 3])
same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5])
the result would be the list [2, 4, 8, 3, 9, 27, 4, 16, 64]. It would first add two to the first. Then two to the second. Then two to the
third, and so on. Hint

Hint Loop through all of the indices of each list using for index in range(len(lst1)) and compare lst1[index] to lst2[index]. Append
index to a new list if those two items are equal.
Use nested for loops. The outer for loop should loop through all of the bases and the inner for loop should loop through all of the
powers. --------------------------

Remember a ** b is a to the power of b Same Values

--------------------------------- same_values(lst1, lst2)

Larger Sum Write a function named same_values() that takes two lists of numbers of equal size as parameters.

larger_sum(lst1, lst2) The function should return a list of the indices where the values were equal in lst1 and lst2.

Create a function named larger_sum() that takes two lists of numbers as parameters named lst1 and lst2. For example, the following code should return [0, 2, 3]

The function should return the list whose elements sum to the greater number. If the sum of the elements of each list are equal, same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5])
return lst1.
Hint
Hint
Loop through all of the indices of each list using for index in range(len(lst1)) and compare lst1[index] to lst2[index]. Append Substring Between
index to a new list if those two items are equal.
substring_between_letters()
--------------------------
Write a function named substring_between_letters that takes a string named word, a single character named start, and another
Reversed List character named end. This function should return the substring between the first occurrence of start and end in word. If start or end
are not in word, the function should return word.
reversed_list(lst1, lst2)
For example, substring_between_letters("apple", "p", "e") should return "pl".
Create a function named reversed_list() that takes two lists of the same size as parameters named lst1 and lst2.
Hint
The function should return True if lst1 is the same as lst2 reversed. The function should return False otherwise.
Begin by finding the indices of the start and end characters by using word.find(start) and word.find(end).
For example, reversed_list([1, 2, 3], [3, 2, 1]) should return True.
If either of those indices are -1, then the original string didn’t contain one of those characters, and you should return word.
Hint
If neither are -1, then slice word using those indices. Remember, slicing is [inclusive:exclusive]!
Let’s say the list are of size 5. You want to compare lst1[0] with lst2[4], lst1[1] with lst2[3] and so on.
------------------------------------------------------------------------
Loop through the numbers created by range(len(lst1)) using a variable named index
X Length
Compare lst1[index] to lst2[len(lst2) - 1 - index]. If those two items are not equal, return False. If you loop through the entrie list
and you never return False, that means that every item was equal, and you should return True. x_length_words()

------------------------- Create a function called x_length_words that takes a string named sentence and an integer named x as parameters. This function
should return True if every word in sentence has a length greater than or equal to x.
strings :
Hint
Count Letters
First create a list of every word in sentence by using sentence.split(). Then iterate through that list and if any of the words have a
unique_english_letters(word) length less than x, return False. If you iterate through all of the words and haven’t returned False, you know every word had a
Write a function called unique_english_letters that takes the string word as a parameter. The function should return the total length greater than or equal to x, so you should return True.
number of unique letters in the string. Uppercase and lowercase letters should be counted as different letters. ------------------------------------------------------------------------
We’ve given you a list of every uppercase and lower case letter in the English alphabet. It will be helpful to include that list in Check Name
your function.
check_for_name()
Hint
Write a function called check_for_name that takes two strings as parameters named sentence and name. The function should
Loop through the list of English letters and check to see if each letter is included in word by using in. return True if name appears in sentence in all lowercase letters, all uppercase letters, or with any mix of uppercase and lowercase
-------------------------------------------------------------- letters. The function should return False otherwise.

Count X For example, the following three calls should all return True:

count_char_x() check_for_name("My name is Jamie", "Jamie")

Write a function named count_char_x that takes a string named word and a single character named x as parameters. The function check_for_name("My name is jamie", "Jamie")
should return the number of times x appears in word. check_for_name("My name is JAMIE", "Jamie")
Hint Hint
Use a for loop to loop through all of the characters of word. If the letter is equal to the value of x, increase a counter variable by name.lower() in sentence.lower() will help you find out if the name is in the sentence.
one.
------------------------------------------------------------------------
------------------------------------------------------------------------
Every Other Letter
Count Multi X
every_other_letter()
count_multi_char_x()
Create a function named every_other_letter that takes a string named word as a parameter. The function should return a string
Write a function named count_multi_char_x that takes a string named word and a string named x. This function should do the containing every other letter in word.
same thing as the count_char_x function you just wrote - it should return the number of times x appears in word. However, this
time, make sure your function works when x is multiple characters long. Hint

For example, count_multi_char_x("Mississippi", "iss") should return 2 The following code will print all letters of a string by index:

Hint my_string = "Hello World"

Consider using the split function. How does the length of word.split(x) relate to the number of times x was in word? for i in range(len(my_string)):

--------------------------------------------------------------------------------- print my_string[i]


In this code, i starts at 0 and increase until it is once less than the length of my_string. How could you make i increase by more
than one each time?

dditionally, instead of printing each individual letter, you should add each letter to a new string using +.

w to use our word with range()?

------------------------------------------------------------------------

8. Every Other Letter

Reverse

reverse_string()

Write a function named reverse_string that has a string named word as a parameter. The function should return word in reverse.

Hint

Just like the last challenge, you want to access each letter of word by it’s index.

my_string = "Hello World"

for i in range(len(my_string)):

print my_string[i]

However, you don’t want i to start at 0. Instead you want it to start at the last index of your string (len(my_string)-1) and end at 0.

Edit the call to the range function to do this. Remember, the range function can take three parameters: the starting number
(inclusive), the ending number (exclusive), and the step. To count down, make the step -1.

------------------------------------------------------------------------

Make Spoonerism

make_spoonerism()

A Spoonerism is an error in speech when the first syllables of two words are switched. For example, a Spoonerism is made when
someone says “Belly Jeans” instead of “Jelly Beans”.

Write a function called make_spoonerism that takes two strings as parameters named word1 and word2. Finding the first syllable
of a word is a difficult task, so for our function, we’re going to switch the first letters of each word. Return the two new words as a
single string separated by a space.

Hint

word2[0] will access the first letter of word2. word1[1:] will access everything but the first letter of word1. Combining those with
a + will give you your first new word.

------------------------------------------------------------------------

Add Exclamation

add_exclamation()

Create a function named add_exclamation that has one parameter named word. This function should add exclamation points to the
end of word until word is 20 characters long. If word is already at least 20 characters long, just return word.

Hint

Use a while loop to add exclamation points to word. The while loop should stop when the length of word is greater than or equal
to 20.

------------------------------------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy