Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help developing code in python. Objective is to formulate these functions d

ID: 3845115 • Letter: N

Question

Need help developing code in python. Objective is to formulate these functions denoted by each part. I want to these functions (parts) to operate in the same series of lines of code.

Part A

Write a function named leap_year that takes one string parameter. It returns True if the string represents a leap year, and returns False otherwise.

For example, 1896, 1904, and 2000 are leap years, but 1900 is not. Therefore, leap_year(‘1896’) returns True.

Part B: Rotate

Write a function rotate(s,n) that has one string parameter s followed by a positive integer parameter n. It returns a rotated string such that the last n characters have been moved to the beginning. If the string is empty or a single character, the function should simply return the string unchanged. Assume that n is less than or equal to the length of s and that n is a positive intger.

For example:

rotate('abcdefgh',3)

returns 'fghabcde'

Part C: Digit Count

Write a function named digit_count that takes one parameter that is a number (int or float) and returns a count of even digits, a count of odd digits, and a count of zeros that are to the left of the decimal point. Return the three counts in that order: even_count, odd_count, zero_count.

For example:

digit_count(1234567890123) returns (5, 7, 1)

digit_count(123400.345) returns (2, 2, 2)

print(digit_count(123.)) returns (1, 2, 0)

print(digit_count(.123)) returns (0, 0, 0)

Part D: Float Check

Write a function named float_check that takes one parameter that is a string and returns True if the string represents a float and False otherwise. For the purpose of this function we define a float to be a string of digits that has at most one decimal point. Note that under this definition an integer argument will return True.

Remember “edge cases” such as “45.” or “.45”; both should return True.

For example:

float_check('1234') returns True

float_check('123.45') returns True

float_check('123.45.67') returns False

float_check('34e46') returns False

float_check('.45') returns True

float_check('45.') returns True

float_check('45..') returns False

again, I need help creating correct codes for each of the parts described above. Once correct codes have been created, the functions must be implemented in the same python suite.

Explanation / Answer

#Part A: Leap Year
def leap_year(s):
    if int(s) % 4 == 0 or int(s) % 400: #If it can be divided by 4 or 400
        return True
    else:
        return False

#Part B: Rotate
def rotate(s, n):
    if len(s) <= 1:
        return s
    else:
        return s[(len(s) - 2):] + s[:(len(s) - 2)] #Rotating using slicing
    #if s = 'akash', then len(s) = 5
    #len(s) - 2 = 3
    #s[(len(s) - 2): ] will return 'sh' i.e., from character 3 to the rest
    #s[:(len(s) - 2)] will return 'aka' i.e., from start to the character before character 3

#Part C: Digit Count
def digit_count(num):
    even_count = 0
    odd_count = 0
    zero_count = 0
    left = True #True until a '.' is found. It indicates if digit is on left of '.'
    for digit in str(num):
        if digit == '.':
            left = False
        elif int(digit) % 2 == 0 and digit != '0':
            even_count += 1
        elif int(digit) % 2 != 0:
            odd_count += 1
        elif digit == '0' and left:
            zero_count += 1

    return even_count, odd_count, zero_count

#Part D: Float Check
def float_check(num):
    if num.count('.') > 1: #If more than one decimal occurs
        return False
    elif num.count('e') > 0: #If contains e
        return False
    else:
        try: #If it can be converted to float
            float(num)
            return True
        except: #else
            return False

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote