Last active
November 5, 2020 05:49
-
-
Save akrisanov/bcc3dfe9890bce0f3a9b64126de279f9 to your computer and use it in GitHub Desktop.
hyperskill.org – Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Round the number from input to the required number of decimals. | |
# | |
# The input format: | |
# Two lines: the first with a floating-point number, the second with an integer representing the decimal count. | |
# | |
# The output format: | |
# A formatted string containing the rounded number. | |
number = float(input()) | |
precision = int(input()) | |
print(f"{number:.{precision}f}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a program that will print the length of a printable representation of the string 'That is \n mine'. | |
# The single quotes are not part of this string. | |
print(len(repr("That is \n mine"))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a program that prints the fourth root from a given real number. | |
# The square root function is located in the math module. Try to find and apply it. | |
# Do not round the result. | |
import math | |
number = float(input()) | |
result = math.pow(number, 1 / 4) | |
print(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You are really good at what you do (programming), so your colleague has decided to congratulate you on the completed project | |
# with a message "You are the best programmer!". But how to do this? | |
# Write the code that prints this message using an escape sequence so that each word of the sentence would be on a new line. | |
print("You are the best programmer!".replace(" ", "\n")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abs_integer = abs(-10) # 10 | |
abs_float = abs(-10.0) # 10.0 | |
round_integer = round(10.0) # 10, returns integer when ndigits is omitted | |
round_float = round(10.2573, 2) # 10.26 | |
pow_integer = pow(2, 10) # 1024 | |
pow_float = pow(2.0, 10) # 1024.0 | |
largest = max(1, 2, 3, 4, 5) # 5 | |
smallest = min(1, 2, 3, 4, 5) # 1 | |
# abs() and pow() functions have equivalents in the math module. | |
# The key difference of math.fabs() and math.pow() is that they always return floats: | |
import math | |
fabs_integer = math.fabs(-10) # 10.0 | |
fabs_float = math.fabs(-10.0) # 10.0 | |
pow_integer = math.pow(2, 10) # 1024.0 | |
pow_float = math.pow(2.0, 10) # 1024.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def my_sqrt(value): | |
""" https://hyperskill.org/learn/step/8242 """ | |
if isinstance(value, str): | |
return "The string should be converted into a numeric data type" | |
elif isinstance(value, (float, int)): | |
return math.sqrt(value) | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment