Skip to content

Commit

Permalink
Add weeks 1-10
Browse files Browse the repository at this point in the history
  • Loading branch information
Lgt2x committed Dec 20, 2020
1 parent 4146de8 commit b75ff03
Show file tree
Hide file tree
Showing 35 changed files with 1,080 additions and 2 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# trainings-2020
INSAlgo 2020-2021 lectures and exercices of ou weekly sessions
# INSAlgo trainings 2020
INSAlgo 2020-2021 lectures and exercises of ou weekly sessions. For your own good, don't read the solution to problems you haven't tried to solve yet.

You can propose your own solutions for missing or improvable ones !

Most slides are inspired by 2018 lectures by [Louis Sugy/Nyrio](https://github.com/Nyrio), credits to him !

## Index
- [Week 01 06/10/20](W01_python) : Discovering Python
- [Week 02 13/10/20](W02_complexity) : Algorithm complexities
- Week 03 20/10/20 : Codingame session, no lecture
- [Week 04 03/11/20](W04_data_structures) : Data structures
- [Week 05 10/11/20](W05_06_dynamic_programming) : Dynamic programming
- [Week 06 17/11/20](W05_06_dynamic_programming) : DP cont'd
- Week 07 24/11/20 : Battledev training, no lecture
- [Week 08 01/12/20](W08_09_graphs) : Introduction to graphs
- [Week 09 08/12/20](W08_09_graphs) : Djikstra algorithm
- [Week 10 15/12/20](W10_minimum_spanning_trees) : Minimum spanning trees
15 changes: 15 additions & 0 deletions W01_python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Week 1 : Introduction to Python
This week, we introduced Python to those of you who didn't know the language yet,
and tried to solve more advanced problems with the remaining people. Here are the problems we gave, and a suggested solution.

## Initiation to Ptyhon (no correction available)
- [Hackerrank's Python beginner Python problems](https://www.hackerrank.com/domains/python)

## Beginner
- [Simple array sum](https://www.hackerrank.com/challenges/simple-array-sum/problem) : [Solution](simple_array_sum.py)
- [Staircase](https://www.hackerrank.com/challenges/staircase/problem) : [Solution](staircase.py)
- [Apple and orange](https://www.hackerrank.com/challenges/apple-and-orange/problem) : [Solution](apple_and_orange.py)

## Medium
- [Greedy florist](https://www.hackerrank.com/challenges/greedy-florist/problem) : [Solution](greedy_florist.py)
- [Sam and substrings](https://www.hackerrank.com/challenges/sam-and-substrings/problem) : [Solution](sam_and_substrings.py)
43 changes: 43 additions & 0 deletions W01_python/apple_and_orange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
st = input().split()

s = int(st[0])

t = int(st[1])

ab = input().split()

a = int(ab[0])

b = int(ab[1])

mn = input().split()

m = int(mn[0])

n = int(mn[1])

apples = list(map(int, input().rstrip().split()))

oranges = list(map(int, input().rstrip().split()))

count_a = 0
for apple in apples:
if s <= a + apple <= t:
count_a += 1

count_b = 0
for orange in oranges:
if s <= b + orange <= t:
count_b += 1

print(count_a)
print(count_b)
42 changes: 42 additions & 0 deletions W01_python/greedy_florist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/python3

import math
import os
import random
import re
import sys


# Complete the getMinimumCost function below.
def getMinimumCost(k, c):
c.sort(reverse=True)
mult = 1
compt = 0
sumC = 0

for item in c:
sumC += mult * item
compt += 1
if compt == k:
compt = 0
mult += 1

return sumC


if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

nk = input().split()

n = int(nk[0])

k = int(nk[1])

c = list(map(int, input().rstrip().split()))

minimumCost = getMinimumCost(k, c)

fptr.write(str(minimumCost) + '\n')

fptr.close()
54 changes: 54 additions & 0 deletions W01_python/sam_and_substrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the substrings function below.
def substrings(n):
"""
Solution de l'exercice en programmation dynamique.
Le principe est d'ajouter graduellement chaque digit et de calculer
les sommes intermédiaires de manière intelligente
Pour mieux comprendre, décortiquons un exemple : 123
Si on cherche à la main les substrings, on trouve 1 2 3 12 23 123
Pas grand chose d'intéressant jusque là.
En ajoutant les digit un par un, calculons les sommes ajoutées à chaque fois.
1 : 1 Somme = 1
12 : 2 12 Somme = 14
123 : 3 23 123 Somme = 149
Total = 164
On voit un pattern apparaître : la somme suivante vaut 10 fois la précédente,
plus le digit ajoutée multiplié par sa place dans la chaîne.
Victoire ! Il ne reste plus qu'à coder :)
"""

MODULO = 10**9 + 7
total = 0
current = 0

for index, digit in enumerate(n):
current = current*10 + (index + 1) * int(digit)
current %= MODULO

total += current
total %= MODULO

return total

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

n = input()

result = substrings(n)

fptr.write(str(result) + '\n')

fptr.close()
23 changes: 23 additions & 0 deletions W01_python/simple_array_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/python3

import os
import sys

#
# Complete the simpleArraySum function below.
#
def simpleArraySum(ar):
return sum(ar)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

ar_count = int(input())

ar = list(map(int, input().rstrip().split()))

result = simpleArraySum(ar)

fptr.write(str(result) + '\n')

fptr.close()
17 changes: 17 additions & 0 deletions W01_python/staircase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the staircase function below.
def staircase(n):
for i in range(n):
print(" "*(n-i-1) + "#"*(i+1))

if __name__ == '__main__':
n = int(input())

staircase(n)
7 changes: 7 additions & 0 deletions W02_complexity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Week 2 : Computational complexity
[This week's slides](complexity_slides.pdf)

## Problems
- [Running time and complexity](https://www.hackerrank.com/challenges/30-running-time-and-complexity) : [Solution](running_time_complexity.py)
- [Sherlock and array](https://www.hackerrank.com/challenges/sherlock-and-array) : [Solution](sherlock_and_array.py)
- [https://www.hackerrank.com/challenges/flatland-space-stations](Flatland space stations) : [Solution](flatland_ss.py)
Binary file added W02_complexity/complexity_slides.pdf
Binary file not shown.
36 changes: 36 additions & 0 deletions W02_complexity/flatland_ss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the flatlandSpaceStations function below.
def flatlandSpaceStations(n, c):
"""
L'astuce à la con : on crée des points virtuels aux deux extrémités
comme si il y avait des stations en miroir de celles au bout pour pouvoir
résoudre l'exercice en faisant simplement un max sur le tableau
"""
c.sort()
c = [-c[0]] + c + [2*(n-1)-c[-1]]

return int(max(c[i]-c[i-1] for i in range(1, len(c)))/2)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

nm = input().split()

n = int(nm[0])

m = int(nm[1])

c = list(map(int, input().rstrip().split()))

result = flatlandSpaceStations(n, c)

fptr.write(str(result) + '\n')

fptr.close()
26 changes: 26 additions & 0 deletions W02_complexity/running_time_complexity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from math import sqrt, floor


def isPrime(nb):
"""
O(sqrt(n)) algorithm to detemine whether or not a number is prime
:param nb: integer, which you want to test primality
:return: True if the number is prime, False otherwise
"""
if nb <= 1:
return False

for i in range(2, floor(sqrt(nb))+1):
if nb % i == 0:
return False

return True


def main():
for _ in range(int(input())):
print("Prime" if isPrime(int(input())) else "Not prime")


if __name__ == "__main__":
main()
50 changes: 50 additions & 0 deletions W02_complexity/sherlock_and_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the balancedSums function below.
def balancedSums(arr):
"""
First, build a cumulative array to avoid computing sums every iteration
We also add a 0 in the first position to be able to test the first element
as a middle element
1 1 4 1 1 becomes
1 2 6 7 8
"""
additive_arr = [0, arr[0]]
for i in range(1, len(arr)):
additive_arr.append(additive_arr[i] + arr[i])

"""
This cumulative list enables us to very easily verify ou property
In our previous example, 4 works because 8 - 6 == 2
"""
total = sum(arr)
for i in range(1, len(additive_arr)):
if additive_arr[-1] - additive_arr[i] == additive_arr[i-1]:
return "YES"

return "NO"



if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

T = int(input().strip())

for T_itr in range(T):
n = int(input().strip())

arr = list(map(int, input().rstrip().split()))

result = balancedSums(arr)

fptr.write(str(result) + '\n')

fptr.close()
34 changes: 34 additions & 0 deletions W04_data_structures/2d_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/python3

import math
import os
import random
import re
import sys


# Complete the hourglassSum function below.
def hourglassSum(arr):
maxi = -800000000000
for x in range(1, 5):
for y in range(1, 5):
maxi = max(maxi, arr[y][x]
+ arr[y - 1][x - 1] + arr[y - 1][x] + arr[y - 1][x + 1]
+ arr[y + 1][x - 1] + arr[y + 1][x] + arr[y + 1][x + 1])

return maxi


if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

arr = []

for _ in range(6):
arr.append(list(map(int, input().rstrip().split())))

result = hourglassSum(arr)

fptr.write(str(result) + '\n')

fptr.close()
15 changes: 15 additions & 0 deletions W04_data_structures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Week 4 : Data structures
[Slides](data_structures_slides.pdf)

## Problems
### Arrays
* [Arrays DS](https://www.hackerrank.com/challenges/arrays-ds/problem) : [Solution](array_ds.py)
* [2D Array](https://www.hackerrank.com/challenges/2d-array/problem) : [Solution](2d_array.py)

### Queues
* [Down to Zero](https://www.hackerrank.com/challenges/down-to-zero-ii/problem) : [Solution](down_to_zero.py)
* [Queue using 2 stacks](https://www.hackerrank.com/challenges/queue-using-two-stacks) : [Solution](queue_stacks.py)

### Stacks
* [Equal stacks](https://www.hackerrank.com/challenges/equal-stacks/problem) : [Solution](equal_stacks.py)
* [Balanced brackets](https://www.hackerrank.com/challenges/balanced-brackets/problem) : [Solution](balanced_brackets.py)
Loading

0 comments on commit b75ff03

Please sign in to comment.