Skip to content

Commit fe36d13

Browse files
let there be code
0 parents  commit fe36d13

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/temp

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Competitive_Programming_Python
2+
3+
This Repo consists of my Python solutions to various problems of Coderbyte, HackerRank, Leetcode, CodeChef etc.
4+
5+
6+
7+
* In the solution, there might be both print statement and return for a function, return is for the testcases of them sites, and print is to get output if we run it outside.

bitmapHoles.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Bitmap_Holes --> CODERBYTE
2+
3+
'''
4+
Have the function BitmapHoles(strArr) take the array of strings stored in strArr,
5+
which will be a 2D matrix of 0 and 1's, and determine how many holes,
6+
or contiguous regions of 0's, exist in the matrix.
7+
A contiguous region is one where there is a connected group of 0's going in
8+
one or more of four directions: up, down, left, or right.
9+
For example: if strArr is ["10111", "10101", "11101", "11111"],
10+
then this looks like the following matrix:
11+
12+
1 0 1 1 1
13+
1 0 1 0 1
14+
1 1 1 0 1
15+
1 1 1 1 1
16+
17+
For the input above, your program should return 2 because there are
18+
two separate contiguous regions of 0's, which create "holes" in the matrix.
19+
You can assume the input will not be empty.
20+
21+
Example1:
22+
Input: ["01111", "01101", "00011", "11110"]
23+
Output: 3
24+
25+
Example2
26+
Input: ["1011", "0010"]
27+
Output: 2
28+
'''
29+
30+
31+
count = 0
32+
def BitmapHoles(strArr):
33+
newList = []
34+
35+
def horizontalCount(ourList):
36+
global count
37+
for each in ourList:
38+
z = list(each)
39+
index = 0
40+
while index < len(z)-1:
41+
if z[index] == '0' and z[index+1] == '0':
42+
count += 1
43+
break
44+
index +=1
45+
46+
def transpose():
47+
newStr = ""
48+
i = 0
49+
while i < len(strArr[0]):
50+
for each in strArr:
51+
newStr += each[i]
52+
# print(newStr)
53+
newList.append(newStr)
54+
newStr = ""
55+
i += 1
56+
57+
# print(f"Sample1: {strArr}")
58+
horizontalCount(strArr)
59+
# print(f"Initial count: {count}")
60+
transpose()
61+
# print(f"Transposed Sample: {newList}")
62+
horizontalCount(newList)
63+
# print(f"Final count: {count}")
64+
return count
65+
66+
print(BitmapHoles(["01111", "01101", "00011", "11110"]))

fizzbuzz.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# FizzBuzz --> LEETCODE
2+
3+
'''
4+
Given an integer n, return a string array answer (1-indexed) where:
5+
answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
6+
answer[i] == "Fizz" if i is divisible by 3.
7+
answer[i] == "Buzz" if i is divisible by 5.
8+
answer[i] == i (as a string) if none of the above conditions are true.
9+
10+
Example 1:
11+
Input: n = 3
12+
Output: ["1","2","Fizz"]
13+
14+
Example 2:
15+
Input: n = 5
16+
Output: ["1","2","Fizz","4","Buzz"]
17+
'''
18+
19+
20+
def fizzBuzz():
21+
list = []
22+
n = int(input("enter n: "))
23+
for each in range(1, n+1):
24+
if each % 3 == 0 and each % 5 ==0:
25+
list.append("FizzBuzz")
26+
elif each% 5 ==0:
27+
list.append("Buzz")
28+
elif each % 3 ==0:
29+
list.append("Fizz")
30+
else:
31+
list.append(str(each))
32+
print(list)
33+
return list
34+
35+
fizzBuzz()
36+

treeConstructor.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Python_tree_constructor (verifier) --> CODERBYTE
2+
3+
'''
4+
TreeConstructor(strArr) take the array of strings stored in strArr,
5+
which will contain pairs of integers in the following format: (i1,i2),
6+
where i1 is child node and i2 represents the parent node.
7+
8+
If strArr can form a proper binary tree, return True. If not, return False
9+
10+
4
11+
/
12+
2
13+
/ \
14+
1 7
15+
16+
Example 1
17+
Input: ["(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"]
18+
Output: true
19+
Example 2
20+
Input: ["(1,2)", "(3,2)", "(2,12)", "(5,2)"]
21+
Output: false
22+
23+
24+
# my_notes:
25+
# (left, right) --> left is the child, and right is the parent of the child
26+
'''
27+
28+
29+
s = ["(1,2)", "(3,2)", "(2,12)", "(5,2)"]
30+
childList = []
31+
parentList = []
32+
33+
34+
def TreeConstructor(s):
35+
for each in s:
36+
childList.append(each[1])
37+
parentList.append(each[3])
38+
# print(childList)
39+
# print(parentList)
40+
41+
countList = dict((x,parentList.count(x)) for x in set(parentList))
42+
# print(countList)
43+
# print(list(countList.values()))
44+
45+
if max(list(countList.values())) >2:
46+
print('FALSE')
47+
return False
48+
else:
49+
print('TRUE')
50+
return True
51+
52+
53+
TreeConstructor(s)

0 commit comments

Comments
 (0)