Skip to content

Instantly share code, notes, and snippets.

@will-fong
Last active August 8, 2024 16:26
Show Gist options
  • Select an option

  • Save will-fong/882f77dbd1f5d7c6c9bba3ba55ba280a to your computer and use it in GitHub Desktop.

Select an option

Save will-fong/882f77dbd1f5d7c6c9bba3ba55ba280a to your computer and use it in GitHub Desktop.
Submissions for HackerRank

HackerRank - Python Exercises

Python

  • Athlete Sort.py
  • Finding the percentage.py
  • ginortS.py
  • Re.start() & Re.end().py
  • Reduce Function.py
  • Regex Substitution.py
  • String Split and Join.py
  • Validating Credit Card Numbers.py
  • Words Score.py

SQL

  • Department Summary.sql
  • New Companies.sql
  • Placements.sql
  • Scheduling Errors.sql
  • The Report.sql
#https://www.hackerrank.com/challenges/python-sort-sort/problem
#!/bin/python3
import math
import os
import random
import re
import sys
if __name__ == '__main__':
nm = input().split()
n = int(nm[0])
m = int(nm[1])
arr = []
for _ in range(n):
arr.append(list(map(int, input().rstrip().split())))
k = int(input())
for j in sorted(arr, key = lambda x: x[k]):
print(*j)
SELECT department.name, COUNT(employee.id) AS numberOfEmployees
FROM department
LEFT JOIN employee ON department.id = employee.dept_id
GROUP BY department.name
ORDER BY numberOFEmployees DESC, department.name ASC
if __name__ == '__main__':
n = int(input())
student_marks = {}
for _ in range(n):
name, *line = input().split()
scores = list(map(float, line))
student_marks[name] = scores
query_name = input()
# extract the values into a list
query_scores = student_marks[query_name]
# sum the scores in the list
total_scores = sum(query_scores)
# convert the floats to decimals and average the scores
from decimal import Decimal
avg = Decimal(total_scores/3)
# Print the mean of the scores, correct to two decimals
print(round(avg,2))
#https://www.hackerrank.com/challenges/ginorts/problem
# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
s = input()
l = sorted(re.findall('[a-z]', s)) + sorted(re.findall('[A-Z]', s)) + sorted(re.findall('[13579]', s)) + sorted(re.findall('[24680]', s))
print(''.join(l))
--https://www.hackerrank.com/challenges/the-company/problem
SELECT
c.company_code
, c.founder
, COUNT(DISTINCT e.lead_manager_code)
, COUNT(DISTINCT e.senior_manager_code)
, COUNT(DISTINCT e.manager_code)
, COUNT(DISTINCT e.employee_code)
FROM company c
INNER JOIN employee e
ON e.company_code = c.company_code
GROUP BY c.company_code, c.founder
ORDER BY c.company_code;
--https://www.hackerrank.com/challenges/placements/problem
SELECT s.name
FROM students s
INNER JOIN friends f
ON s.id = f.id
INNER JOIN packages p_student
ON s.id = p_student.id
INNER JOIN packages p_friend
ON f.friend_id = p_friend.id
WHERE
1 = 1
AND p_student.salary < p_friend.salary
ORDER BY p_friend.salary;
# https://www.hackerrank.com/challenges/re-start-re-end/problem
import re
S = input()
k = input()
m = re.search(k, S)
pattern = re.compile(k)
if not m:
print("(-1, -1)")
while m:
print("({0}, {1})".format(m.start(), m.end()-1))
m = pattern.search(S, m.start()+1)
#https://www.hackerrank.com/challenges/reduce-function/problem
from fractions import Fraction
from functools import reduce
def product(fracs):
t = reduce(lambda fraction1,fraction2:fraction1* fraction2,fracs)
return t.numerator, t.denominator
if __name__ == '__main__':
#https://www.hackerrank.com/challenges/re-sub-regex-substitution/problem
# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
s = int(input())
for i in range(s):
pattern1 = re.compile(r'(?<= )(&&)(?= )')
pattern2 = re.compile(r'(?<= )(\|\|)(?= )')
print(pattern2.sub('or', pattern1.sub('and', input())))
SELECT DISTINCT prof.name AS "PROFESSOR.NAME", cou.name AS "COURSE.NAME"
FROM professor prof
INNER JOIN schedule sch
ON sch.professor_id = prof.id
INNER JOIN course cou
ON sch.course_id = cou.id
WHERE cou.department_id <> prof.department_id;
#https://www.hackerrank.com/challenges/python-string-split-and-join/problem
def split_and_join(line):
a = line.split(" ")
a = "-".join(a)
return a
if __name__ == '__main__':
line = input()
result = split_and_join(line)
print(result)
--https://www.hackerrank.com/challenges/the-report/problem
SELECT IF(GRADE < 8, NULL, NAME), GRADE, MARKS
FROM STUDENTS INNER JOIN GRADES
WHERE
1 = 1
AND MARKS BETWEEN MIN_MARK AND MAX_MARK
ORDER BY GRADE DESC, NAME
#https://www.hackerrank.com/challenges/validating-credit-card-number/problem
# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
valid_card = r"^[456]\d{3}(-\d{4}){3}$|^[456]\d{15}$"
no_repeats = r"(\d)(-?\1){3}"
for i in range(int(input())):
card = input()
print(f'{"Valid" if (re.match(valid_card, card) and not re.search(no_repeats, card)) else "Invalid"}')
#https://www.hackerrank.com/challenges/words-score/problem
def is_vowel(letter):
return letter in ['a', 'e', 'i', 'o', 'u', 'y']
def score_words(words):
score = 0
for word in words:
num_vowels = 0
for letter in word:
if is_vowel(letter):
num_vowels += 1
if num_vowels % 2 == 0:
score += 2
else:
score += 1
return score
n = int(input())
words = input().split()
print(score_words(words))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment