Skip to content

Instantly share code, notes, and snippets.

View BharathKumarS's full-sized avatar
:octocat:
Committed to constant learning

Bharath Kumar Shivakumar BharathKumarS

:octocat:
Committed to constant learning
View GitHub Profile
@BharathKumarS
BharathKumarS / VirginMatrix.py
Last active February 16, 2020 22:35
Partial solution to build a Magic matrix of length 3
import os
import random
import re
import sys
import numpy as np
def VirginSquare(sq):
host = np.zeros(shape=(3,3))
missing = list()
index = list()
@BharathKumarS
BharathKumarS / Two Sum.py
Last active February 17, 2020 19:23
This was my first Leet Code attempt, Two Sum.
#Solution works only on LeetCode
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
anums = nums
for i in range(len(nums)-1):
for j in range(i+1,len(anums)):
if nums[i] + anums[j] == target:
return[i,j]
@BharathKumarS
BharathKumarS / Reverse Integer
Created February 17, 2020 19:25
Reverse an integer with -2**31 through 2**31 - 1 constraint
#Solution works only on LeetCode
class Solution:
def reverse(self, x: int) -> int:
if not x < 0:
y = int(str(x)[::-1])
else:
x = x * -1
y = int(str(x)[::-1]) * -1
if y <= -2**31 or y >= 2**31 - 1:
@BharathKumarS
BharathKumarS / Roman_Integer.py
Created February 18, 2020 14:02
Roman number to Integer. Very efficient with Time and Space complexity
#This solution works only when executed on LeetCode. For test cases and practice, check my git repo
class Solution:
def romanToInt(self, s: str) -> int:
Guide = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D': 500, 'M':1000}
num = prev_char = 0
for char in s[::-1]:
if Guide[char] >= prev_char:
prev_char = Guide[char]
num = num + Guide[char]
else:
@BharathKumarS
BharathKumarS / Return_Palindrome.py
Last active February 27, 2020 22:02
LeetCode, Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
#This solution works only when executed on LeetCode. For test cases and practice, check my git repo
class Solution:
def isPalindrome(self, x: int) -> bool:
rev = ''
for char in str(x)[::-1]:
rev = rev + char
if str(x) == rev:
return(True)
else:
return(False)
@BharathKumarS
BharathKumarS / Sharing_Choco.py
Created February 19, 2020 05:50
Birthday Chocolates from HackerRank
#This is a test case check. Please check my git repo for the actual solution to go into HackerRank
def choco(s,d,m):
can = 0
if len(s) > 1:
for sq in range(len(s)-m):
if sum(s[sq:sq+m]) == d:
can += 1
else:
if s[0] == d:
can = 1
@BharathKumarS
BharathKumarS / Migrating Birds.py
Created February 20, 2020 20:40
Find the frequent bird species with least integer value
def migrate(birds):
birds.sort()
FFB = my_bird = count = 0
for bird in range(len(birds)-1):
if birds[bird] == birds[bird+1]:
count += 1
if count > my_bird:
my_bird = count
FFB = birds[bird]
else:
@BharathKumarS
BharathKumarS / Paired_Sum.py
Created February 21, 2020 18:57
Divisible sum pair - Program to check the sum pair matching the given number
# This is a test case, please check my git rrepo for the code that goes on HackerRank
def pair(a,num):
b = a
count = 0
for i in range(len(a)-1):
for j in range(i+1,len(b)):
if (a[i] + b[j]) % num == 0:
count += 1
return count
@BharathKumarS
BharathKumarS / Travel_from_Russia.py
Last active February 27, 2020 22:00
HackerRank, print a string representing the date of the 256th day of the year given.
# Day of the coder from HackerRank
def russia(year):
days = 256
sams = 0
months = [31,28,31,30,31,30,31,31,30]
if 1700 <= year <= 1917:
months[8] = months[8] - 13
if (year % 4) == 0:
months[1] = months[1] + 1
elif year == 1918:
@BharathKumarS
BharathKumarS / Check_Palindrome.py
Created February 22, 2020 22:08
LeetCode - Check for Palindrome - Check for 3 line code solution on my git repo
#Can you believe I wrote another solution with just 3 lines of code? Check my Git repo for the actual code!
class Palindrome:
def text(self, set_of_chars):
rev = clean = ''
for char in set_of_chars[::-1]:
if char.isalnum():
rev = rev + char.lower()
for ele in set_of_chars[::1]: