Question

Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n):...

Implement the following functions in the given code:

def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n): def is_leap_year(year): def get_letter_grade_version_1(x): def get_letter_grade_version_2(x): def get_letter_grade_version_3(x):

Pay careful attention to the three different versions of the number-grade-to-letter-grade functions. Their behaviors are subtly different.

Use the function descriptions provided in the code to replace the pass keyword with the necessary code.

Remember: Parameters contain values that are passed in by the caller. You will need to make use of the parameters that each function has. Do not change the parameters. Do not change the names of the parameters and do not change the values of the parameters. Just make use of the values given to you.

When you run your program it will load, execute, and finish. Nothing will display unless you write test code outside of the functions.

Read all of the comments in the starter very carefully. All the necessary requirements are written out for you.

Starter code:

#

# I am importing this module so we can use its functions. # import random # Reference: https://docs.python.org/3/library/random.html

# ---------------------------------------------------------

# ---------------------------------------------------------

#

# Poker cards are numbered 1 through 13 (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King)

# and come in four suits: Spades, Hearts, Clubs, Diamonds.

#

# random_suit_number() returns a number 0 <= N < 4. Section 4.5 of your textbook covers this.

#

# get_card_suit_string_from_number(n) takes an integer and returns a string with the name of a suit:

# 0 ---> 'Spades'

# 1 ---> 'Hearts'

# 2 ---> 'Clubs'

# 3 ---> 'Diamonds'

# Note that get_card_suit_string_from_number(n) should return None if n is not a valid value. def random_suit_number(): ''' Returns a random integer N such that 0 <= N < 4. ''' pass def get_card_suit_string_from_number(n): ''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. ''' pass

# Expected results:

# get_card_suit_string_from_number(1) ----> 'Hearts'

# get_card_suit_string_from_number(5) ----> None

# get_card_suit_string_from_number(-1) ----> None

#

# ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

# Poker cards are numbered 1 through 13 (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King)

# and come in four suits: Spades, Hearts, Clubs, Diamonds.

#

# random_value_number() returns a number 1 <= N <= 13. Section 4.5 of your textbook covers this.

#

# get_card_value_string_from_number(n) takes an integer and returns a string with the name of a card value:

# 1 ---> 'Ace' # 2 ---> '2' # 3 ---> '3' # ... # 10 ---> '10' # 11 ---> 'Jack' # 12 ---> 'Queen' # 13 ---> 'King'

# Note that get_card_value_string_from_number(n) should return None if n is not a valid value. def random_value_number(): pass def get_card_value_string_from_number(n): pass

# Expected results: # get_card_value_string_from_number(12) ---> 'Queen'

# get_card_value_string_from_number(7) ---> '7'

# get_card_value_string_from_number(-7) ---> None

# get_card_value_string_from_number(0) ---> None

# get_card_value_string_from_number(14) ---> None

# ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

#

# The rules for determining whether a year is a leap year (it has # February 29 on the calendar) are a little strange:

# - a year is a leap year if it is a multiple of 4

# - unless it is a multiple of 100, in which case it is NOT a leap year

# - unless it is a multiple of 400, in which case it IS a leap year def is_leap_year(year): ''' Return True if year is a leap year, or False otherwise. ''' pass

#

# Expected results:

# is_leap_year(2000) ----> True

# is_leap_year(2001) ----> False

# is_leap_year(2012) ----> True

# is_leap_year(2040) ----> True

# is_leap_year(2100) ----> False

# is_leap_year(2104) ----> True

# is_leap_year(2200) ----> False

# is_leap_year(2400) ----> True

# is_leap_year(3000) ----> False

#

# ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

#

# Convert a numerical grade to a letter grade using the following chart:

# A 90 and higher

# B 80 to 89

# C 70 t0 79

# D 60 to 69

# F 0 to 59 def get_letter_grade_version_1(x): ''' Return the letter grade corresponding to the given numerical grade. '''

# Assume all inputs are valid and do not do any extra work. pass

# Expected results:

# get_letter_grade_version_1(85) ----> 'B'

# get_letter_grade_version_1(43) ----> 'F'

# get_letter_grade_version_1(104) ----> 'A'

# get_letter_grade_version_1(79.9) ----> 'C'

# get_letter_grade_version_1(70.3) ----> 'C'

# get_letter_grade_version_1('70') ----> should cause an error

# get_letter_grade_version_1('AB') ----> should cause an error

#

# ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

#

# Same grade chart as above, but check whether x is an integer.

# If x is not an integer then return None. def get_letter_grade_version_2(x): ''' Return the letter grade corresponding to the given numerical grade, or None if x is not an integer. ''' pass

# Expected results: # get_letter_grade_version_2(85) ----> 'B'

# get_letter_grade_version_2(43) ----> 'F'

# get_letter_grade_version_2(104) ----> 'A'

# get_letter_grade_version_2(79.9) ----> None

# get_letter_grade_version_2(70.3) ----> None

# get_letter_grade_version_2('70') ----> None

# get_letter_grade_version_2('AB') ----> None

# # ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

# ---------------------------------------------------------

#

# Same grade chart as above, but do what you can to interpret

# x as a number and to round to the nearest whole number.

# Return None if you cannot interpret x as a number. def get_letter_grade_version_3(x): ''' Return the letter grade corresponding to the given numerical grade, given the instructions. ''' pass

# Expected results:

# get_letter_grade_version_3(85) ----> 'B'

# get_letter_grade_version_3(43) ----> 'F'

# get_letter_grade_version_3(104) ----> 'A'

# get_letter_grade_version_3(79.9) ----> 'B'

# get_letter_grade_version_3(70.3) ----> 'C'

# get_letter_grade_version_3('70') ----> 'C'

# get_letter_grade_version_3('AB') ----> None

# # ---------------------------------------------------------

# ---------------------------------------------------------

Homework Answers

Answer #1

Here is the Python code:

import random

def random_suit_number ():
    return random.randint (0, 3)
    
def get_card_suit_string_from_number(n):
    if n == 0:  return ('Spades')
    if n == 1:  return ('Hearts')
    if n == 2:  return ('Clubs')
    if n == 3:  return ('Diamonds')
    return ('None')
    
def random_value_number():
    return random.randint (1, 13)
    
def get_card_value_string_from_number(n):
    if n == 1: return 'Ace'
    if n == 11: return 'Jack'
    if n == 12: return 'Queen'
    if n == 13: return 'King'
    if n >= 2 and n <= 10: return str(n)
    return ('None')

def is_leap_year(year):
    if year % 400 == 0: return True
    if year % 100 == 0: return False
    if year % 4 == 0: return True
    return False
    
def get_letter_grade_version_1(x):
    if x >= 90: return 'A'
    if x >= 80: return 'B'
    if x >= 70: return 'C'
    if x >= 60: return 'D'
    return 'F'
    
def get_letter_grade_version_2(x):
    if not isinstance (x, int) and not isinstance (x, float) : return 'None'
    return get_letter_grade_version_1(x)
    
def get_letter_grade_version_3(x):
    try:
        x = float(x)
    except ValueError:
        return 'None'
    return get_letter_grade_version_2(x)
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
# given a radius, find the area of a circle def get_area_circle(r):        """        ...
# given a radius, find the area of a circle def get_area_circle(r):        """         what it takes             radius of a circle (any positive real number)         what it does:             computes the area of a circle             given a radius, find the area of a circle             area = pi*r2 (pi times r squared)         what it returns             area of a circle. Any positive real number (float)     """         # your code goes in...
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
# Fill in the missing parts in the following code. # It should compute the same...
# Fill in the missing parts in the following code. # It should compute the same result as map(lambda x: n*x, L). def multAll(n, L): '''Assuming n is a number and L is a list of numbers, make a list by multiplying each element of L by n. For example, multAll(3,[3,5,7,9]) is [9,15,21,27].''' if L==[]: return None # TO-DO replace None else: return None # TO-DO replace None
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS...
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS = 5000 # generates a random n-queens board # representation: a list of length n the value at index i is # row that contains the ith queen; # exampe for 4-queens: [0,2,0,3] means that the queen in column 0 is # sitting in row 0, the queen in colum 1 is in row, the queen in column 2 # is in row 0,...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n,...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n, a positive integer, it returns a list of n tuples corresponding to the numbers 1 through n (both inclusive): the tuple for the number k consists of k as the first component, and exactly one of the following strings as the second: • the string 'two!' if k is divisible by 2 • the string 'three!' if k is divisible by 3 • the...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT