The rectangle with given perimeter and area if and only if both sides are integer lengths. In
Question:
The rectangle with given perimeter and area if and only if both sides are integer lengths. In a rectangle ???????????????????????????????????? = 2???? + 2ℎ and ???????????????? = ???? ∗ ℎ. Only one loop is allowed.
Preconditions and Postconditions perimeter: int area: int
Returns: int -> The longest side length for rectangle of integer lengths False -> No rectangle exists that meets the requirements
Allowed methods, operators, and libraries (if it is not in this list, you cannot use it):
- Loops, conditionals, all arithmetic operations
- math.sqrt from the math module
- Type conversion int()
- Integer division (also known as floor division), using the // operator. Floor division, as opposed to true division, discards any fractional result from the output.
o 5//2 returns 5
- The built-in float.is_integer() method returns True if the float instance is finite with integral value, and False otherwise o 5.35.is_integer() returns False o 5.0.is_integer() returns True
- The built-in round(number, digits) method returns a floating-point number rounded to the specified number of decimals o round(3.14159, 1) returns 3.1 o round(3.14159, 2) returns 3.14 o round(3.14159, 0) returns 3.0
Examples:
>>> rectangle(14, 10) # Represents a 2x5 rectangle
5 # 5 is the larger of the two sides
>>> rectangle(25, 25) # Represents a 2.5x10 rectangle
False # 2.5 is not an integer
Give me those digits...
When you are given a sequence of numbers such as [6, 7, 5], printing each element from right to left can be achieved with a simple loop:
lst = [6, 7, 5] for i in range(len(lst)-1, -1, -1):
print(lst[i])
5
7
6
But what if we want to print each digit from a given number such as 675? To start, integers do not have a len method to get the number of digits.
>>> len(675)
TypeError: object of type 'int' has no len()
Additionally, integers are not iterable!
for i in 675: print(i)
TypeError: 'int' object is not iterable
To process digits in a positive integer, modulo (%) 10 and floor division (//) by 10 can help us extract and remove the least significant digit:
>>> num = 675
>>> print(num%10) 5
Floor division by 10 removes the rightmost digit (675//10 =
>>> num = num//10
>>> num 67), while modulo 10 returns the rightmost digit (675%10 = 5). 67 This set of operations combined with a while loop will allow
>>> print(num%10) you to traverse the integer from right to left.
7 Can you spot the continuation condition of the loop?
>>> num = num//10
>>> num
6
>>> print(num%10) 6
>>> num = num//10
>>> num
0
Given a number oct_num in octal system (base 8), return oct_num in decimal system (base 10). In the octal number system, each digit represents a power of eight and it uses the digits 1 to 7. To convert a number represented in octal system to a number represented in decimal system, each digit must be multiplied by the appropriate power of eight. For example, given the octal number 2068 results in the decimal number 13410:
2 0 6 206 = (2 × 8²) + (0 × 8¹) + (6 × 8⁰) = 134
82 81 80
• You are NOT allowed to type convert num to a string
Preconditions and Postconditions
oct_num: int -> Positive number that always starts with a digit in range 1-7 Returns: int -> decimal representation of oct_num
Example:
>>> to_decimal(237) # (82 * 2) + (81 * 3) + (80 * 7) 159 >>> to_decimal(35) # (81 * 3) + (80 * 5) 29 |
A given integer num has a hoagie if at least a digit in num is surrounded by two identical digits. This function takes an integer and returns True is num has a hoagie, False otherwise. Note that num//10 and num%10 will not behave as intended when num is negative, so make sure your process negative numbers as positive.
Tip: A number that has less than three digits cannot have a hoagie.
• You are NOT allowed to type convert num to a string
Preconditions and Postconditions num: int -> any integer value
Returns: bool -> True is num has a hoagie, False otherwise
Example:
>>> has_hoagie(737) # 737
True
>>> has_hoagie(35)
False
>>> has_hoagie(-6060) # 606 or 060
True
>>> has_hoagie(-111) # 111
True
>>> has_hoagie(6945) False
Given a list of lists lsts, where each list contains numbers only, return a new list with all the numbers that appear in all the lists in lsts. You can assume that lsts contains at least one list. Tip: You can check if an element is in a list with the in operator
Preconditions and Postconditions
lsts: list -> Integer smaller or greater than 1
Returns: list -> non-empty sequence of integers
Examples:
>>> joint_lst([[75.5, 1, 2, 3], [1, 3, 5], [0, 5, 9, 3, 1, -96, 8, 1]]) [1, 3] >>> joint_lst([[1, 2, 3], [4, 5], [7, 8, 9, 10], [6, 89]]) [] |
________________________________________________________________________________________________________________________________________________________________________________________________________
def rectangle(perimeter,area):
"""
>>> rectangle(14, 10)
5
>>> rectangle(12, 5)
5
>>> rectangle(25, 25)
False
>>> rectangle(50, 100)
20
>>> rectangle(11, 5)
False
>>> rectangle(11, 4)
False
"""
#- YOUR CODE STARTS HERE
pass
def to_decimal(oct_num):
"""
>>> to_decimal(237)
159
>>> to_decimal(35)
29
>>> to_decimal(600)
384
>>> to_decimal(420)
272
"""
#- YOUR CODE STARTS HERE
pass
def has_hoagie(num):
"""
>>> has_hoagie(737)
True
>>> has_hoagie(35)
False
>>> has_hoagie(-6060)
True
>>> has_hoagie(-111)
True
>>> has_hoagie(6945)
False
"""
#- YOUR CODE STARTS HERE
pass
def joint_lst(lsts):
"""
>>> joint_lst([[75.5, 1, 2, 3], [1, 3, 5], [0, 5, 9, 3, 1, -96, 8, 1]])
[1, 3]
>>> joint_lst([[1, 2, 3], [4, 5], [7, 8, 9, 10], [6, 89]])
[]
"""
#- YOUR CODE STARTS HERE
pass