-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonCodingInterviewQuestions.py
65 lines (49 loc) · 1.29 KB
/
PythonCodingInterviewQuestions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Python Coding Interview Questions
# How can you replace string space with a given character in Python?
def replace_string(text, replacementCharacter):
result = ''
for i in text:
if i == ' ':
i = replacementCharacter
result += i
return result
replace_string("He loWor d", "l")
# "HelloWorld"
replace_string("He lo", "l")
# "Hello"
replace_string("Hello", "l")
# "Hello"
replace_string("He o", "l")
# "Hello"
# Given a positive integer num, write a function that returns True if num is a perfect square else False.
def valid_square(number):
# turns square of number into int
square = int(number**0.5)
# square the int (number rounds down to 0)
# if this equal the number passed in the new have a square roto
return square**2 == number
valid_square(10)
# False
valid_square(36)
# True
valid_square(4.0)
# False
# Given an integer n, return the number of trailing zeroes in n factorial n!
def count_trailing_zeros(n):
# factorial ex: 5 * 4 * 3 * 2 * 1
fact = n
while n > 1:
fact *= n - 1
n -= 1
# Get trailing zeros
result = 0
for i in str(fact)[::-1]:
if i == '0':
result += 1
else:
break
return result
count_trailing_zeros(10)
# 2
count_trailing_zeros(18)
# 3