Given an integer n, find the minimal k such that
k=m! (wherem! =12... *m)for some integer m;k≥n. In other
words, find the smallest factorial which is not less than n.
Constraints
1 ≤ n ≤ 120
Solution
def smallestFactorial(n):
if n>=1 and n<=120:
fact = 1
for i in range(2, n+1):
fact = fact*i
if fact >= n:
return fact
else:
return -1You own a lipstick business. When a lipstick container is empty, there is actually some leftover lipstick at the bottom that cannot be used because it is not accessible. Being an environmentally friendly business owner, you would like to recycle the leftover lipstick to make more. As a business, you know you need ‘numberOfLeftoversNeeded‘ to make a new lipstick. You have ‘numberOfLipsticks‘ in your possession. What’s the total number of lipsticks you can sell assuming that each of your customers return their leftovers?
For numberOfLipsticks = 5 and numberOfLeftoversNeeded = 2 the output should be getTotalNumberOfLipsticks(numberOfLipsticks, numberOfLeftover- sNeeded) = 9
Here is how you get 9 lipsticks: Sell 5 lipsticks, get 5 leftovers; Create 2 more lipsticks using 4 leftovers (1 leftover remains); Sell 2 lipsticks, end up with 3 leftovers; Create 1 lipstick using 2 leftovers (1 leftover remains); Sell 1 lipstick, end up with 2 leftovers; Create 1 lipstick using 2 leftovers (no leftovers remain); Sell 1 lipstick.
Thus you sell 5 + 2 + 1 + 1 = 9 lipsticks!
Solution
def getTotalNumberOfLipsticks(numberOfLipsticks, numberOfLeftoversNeeded):
if numberOfLeftoversNeeded <= 0 or numberOfLipsticks <= 0:
return 0
total = numberOfLipsticks
newLipsticks = 0
while numberOfLipsticks >= numberOfLeftoversNeeded:
createLipstick = int(numberOfLipsticks/numberOfLeftoversNeeded)
unusedLeftover = (numberOfLipsticks % numberOfLeftoversNeeded)
numberOfLipsticks = createLipstick + unusedLeftover
newLipsticks += createLipstick
return total+newLipsticksA school teacher wants to hand out treats to his students. The teacher decides the best way to divide the treats is to have the students sit in a circle of sequentially numbered chairs. A chair number will be drawn from a hat. Begin- ning with the student in drawn chair, one treat will be handed to each student sequentially going around the circle until all treats have been distributed.
The teacher wants to have the students involved in sharing treats. He decides that whoever gets the very last treat, will be the student who makes the treats for the next game. Determine the chair number occupied by the student who will receive the last treat.
For example, there are 4 students and 6 treats. The students arrange them- selves in seats numbered 1 to 4. Let’s suppose 2 is drawn from the hat. Students receive treats at positions 2,3,4,1,2,3. The student who gets the last treat is in chair number 3.
function getLastStudent(numberOfStudents, treats, startingChair) {...}
Constraints
Solution
def getLastStudent(numberOfStudents, treats, startingChair):
if numberOfStudents>0 and startingChair>0 and startingChair<=numberOfStudents and treats>0:
n = startingChair + ((treats-1)*1)
idx = n % numberOfStudents
if idx == 0:
return startingChair
return idx
else:
return -1function peopleWatch(heightOfPeople){...}
You are people watching at the mall and you notice that the Cinnabon line has people of various heights. Write a program that can accept a list of numbers for the height of each person in the line and returns the index for the next person in line that is taller than the person at that position. If there is no person taller than them in the line, return null (or in python None).
Examples
Input: [5.5, 4.5, 4, 6, 3.3]
Output: [3, 3, 3, null, null]
Input: [6, 4.5, 5.5, 4, 6, 3.3]
Output:[null, 2, 4, 4, null, null]
Solution
def peopleWatch(heightOfPeople):
res = []
start = 0
end = len(heightOfPeople)-1
store_idx = 0
flg = False
while(start<end):
max_height = heightOfPeople[start]
if heightOfPeople[end] > max_height:
store_idx = end
flg = True
if end == start+1:
if flg:
res.append(store_idx)
else:
res.append(None)
start = start + 1
end = len(heightOfPeople)-1
flg = False
else:
end = end-1
res.append(None)
return res