Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:

answer = dict()

for k, v in enumerate(nums):

if v in answer:
return [answer[v], k]
else:
answer[target - v] = k

return []





Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
import re

class Solution:
def isPalindrome(self, s: str) -> bool:

# To lowercase
s = s.lower()

# Remove non-alphanumeric characters
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)

# Determine if s is palindrome or not
len_s = len(s)

for i in range(len_s//2):

if s[i] != s[len_s - 1 - i]:
return False

return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def trailingZeroes(self, n: int) -> int:
"""
Count trailing zeroes in n! by counting factors of 5.

Key insight: Trailing zeroes come from 10 = 2 * 5.
Since there are always more factors of 2 than 5 in n!,
we just need to count factors of 5.

We count: floor(n/5) + floor(n/25) + floor(n/125) + ...
This counts all multiples of 5, 25, 125, etc.

Time complexity: O(log n) - we divide by 5 each iteration
Space complexity: O(1)
"""
count = 0
while n > 0:
n //= 5
count += n
return count
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function trailingZeroes(n: number): number {
// Count trailing zeroes in n! by counting factors of 5
// Trailing zeroes come from 10 = 2 * 5
// Since there are always more factors of 2 than 5 in n!,
// we just need to count factors of 5
//
// We count: floor(n/5) + floor(n/25) + floor(n/125) + ...
// Time: O(log n), Space: O(1)

let count = 0;
while (n > 0) {
n = Math.floor(n / 5);
count += n;
}
return count;
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_128_factorial_trailing_zeroes import Solution

class FactorialTrailingZeroesTestCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()

def test_example_1(self):
# 3! = 6, no trailing zero
self.assertEqual(self.solution.trailingZeroes(3), 0)

def test_example_2(self):
# 5! = 120, one trailing zero
self.assertEqual(self.solution.trailingZeroes(5), 1)

def test_example_3(self):
# 0! = 1, no trailing zero
self.assertEqual(self.solution.trailingZeroes(0), 0)

def test_larger_number(self):
# 10! = 3628800, two trailing zeroes
self.assertEqual(self.solution.trailingZeroes(10), 2)

def test_multiple_of_25(self):
# 25! has floor(25/5) + floor(25/25) = 5 + 1 = 6 trailing zeroes
self.assertEqual(self.solution.trailingZeroes(25), 6)

def test_large_number(self):
# 100! has many trailing zeroes
# floor(100/5) + floor(100/25) + floor(100/125) = 20 + 4 + 0 = 24
self.assertEqual(self.solution.trailingZeroes(100), 24)

def test_single_digit(self):
# 4! = 24, no trailing zero
self.assertEqual(self.solution.trailingZeroes(4), 0)


if __name__ == "__main__":
unittest.main()
Loading