Skip to content

Commit 5005495

Browse files
committed
add certificate: add final certificate & solutions
1 parent 23823cb commit 5005495

9 files changed

+458
-1
lines changed

Check Difference.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def f1():
2+
x+=1
3+
print(x)
4+
5+
x=12
6+
f1()
7+
print("x")

README.md

Lines changed: 232 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 21-Day Coding Challenge: Ninja Slayground 2.0
22

3-
Repositorio con el código solución a los desafíos diarios.
3+
Repositorio con el código solución a los 21 desafíos diarios.
44

55
## Tabla de Contenido
66

@@ -10,6 +10,7 @@ Repositorio con el código solución a los desafíos diarios.
1010
- [Certificado Participación](#certificado-participación)
1111
- [Certificado Level 1: 7 days Streak](#certificado-level-1-7-days-streak)
1212
- [Certificado Level 2: 14 days Streak](#certificado-level-2-14-days-streak)
13+
- [Certificado Level 3: 21 days Streak](#certificado-level-3-21-days-streak)
1314
- [Ejercicios Resueltos](#ejercicios-resueltos)
1415
- [Day 1: Switch Case statement](#day-1-switch-case-statement)
1516
- [Day 2: Nth Fibonacci Number](#day-2-nth-fibonacci-number)
@@ -26,6 +27,12 @@ Repositorio con el código solución a los desafíos diarios.
2627
- [Day 13: Binary Search](#day-13-binary-search)
2728
- [Day 14: Sort An Array of 0s, 1s and 2s](#day-14-sort-an-array-of-0s-1s-and-2s)
2829
- [Day 15: Implement Lower Bound](#day-15-implement-lower-bound)
30+
- [Day 16: Valid Parentheses](#day-16-valid-parentheses)
31+
- [Day 17: Implement Upper Bound](#day-17-implement-upper-bound)
32+
- [Day 18: Spiral Matrix](#day-18-spiral-matrix)
33+
- [Day 19: Zero Matrix](#day-19-zero-matrix)
34+
- [Day 20: Pascal's Triangle](#day-20-pascals-triangle)
35+
- [Day 21: Number of Inversions](#day-21-number-of-inversions)
2936

3037
## Certificados Obtenidos
3138

@@ -41,6 +48,10 @@ Repositorio con el código solución a los desafíos diarios.
4148

4249
![Certificado Level 2: 14 days Streak](./certificados/certificado_level2.webp)
4350

51+
### Certificado Level 3: 21 days Streak
52+
53+
![Certificado Level 3: 21 days Streak](./certificados/certificado_level3.webp)
54+
4455
## Ejercicios Resueltos
4556

4657
### Day 1: Switch Case statement
@@ -417,3 +428,223 @@ def lowerBound(arr: [int], n: int, x: int) -> int:
417428
l=mid+1
418429
return l
419430
```
431+
432+
### Day 16: Valid Parentheses
433+
434+
You're given a string 'S' consisting of "{", "}", "(", ")", "[" and "]" .
435+
436+
Return true if the given string 'S' is balanced, else return false.
437+
438+
For example:
439+
'S' = "{}()".
440+
441+
There is always an opening brace before a closing brace i.e. '{' before '}', '(' before ').
442+
So the 'S' is Balanced.
443+
444+
```py
445+
def isValidParenthesis(s: str) -> bool:
446+
# Write your code here
447+
stack=[]
448+
for c in s:
449+
if c=='(' or c=='[' or c=='{':
450+
stack.append(c)
451+
elif c==')':
452+
if stack and stack[-1]=='(':
453+
stack.pop()
454+
else:
455+
return False
456+
elif c==']':
457+
if stack and stack[-1]=='[':
458+
stack.pop()
459+
else:
460+
return False
461+
elif c=='}':
462+
if stack and stack[-1]=='{':
463+
stack.pop()
464+
else:
465+
return False
466+
return not stack
467+
```
468+
469+
### Day 17: Implement Upper Bound
470+
471+
You are given a sorted array ‘arr’ containing ‘n’ integers and an integer ‘x’.Implement the ‘upper bound’ function to find the index of the upper bound of 'x' in the array.
472+
473+
Note:
474+
475+
1. The upper bound in a sorted array is the index of the first value that is greater than a given value.
476+
2. If the greater value does not exist then the answer is 'n', Where 'n' is the size of the array.
477+
3. Try to write a solution that runs in log(n) time complexity.
478+
479+
Example:
480+
481+
Input : ‘arr’ = {2,4,6,7} and ‘x’ = 5,
482+
483+
Output: 2
484+
485+
Explanation: The upper bound of 5 is 6 in the given array, which is at index 2 (0-indexed).
486+
487+
```py
488+
from typing import List
489+
490+
def upperBound(arr: List[int], x: int, n: int) -> int:
491+
# Write your code here.
492+
l,r=0,n-1
493+
while l<=r:
494+
mid=(l+r)//2
495+
if arr[mid]==x and (arr[mid+1]>x if mid<n-1 else True):
496+
return mid+1
497+
elif arr[mid]>x:
498+
r=mid-1
499+
else:
500+
l=mid+1
501+
return l
502+
```
503+
504+
### Day 18: Spiral Matrix
505+
506+
You are given a 2D matrix ‘MATRIX’ of ‘N’\*’M’ dimension. You have to return the spiral traversal of the matrix.
507+
508+
Example:
509+
510+
Input:
511+
MATRIX = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60] ]
512+
Output:
513+
1 3 5 7 20 60 34 30 23 10 11 16
514+
515+
Explanation: Starting from the element in the first row and the first column, traverse from left to right (1 3 5 7), then top to bottom (20 60), then right to left (34 30 23), then bottom to up (10) and then left to right (11 16).
516+
517+
```py
518+
def spiralMatrix(matrix : List[List[int]]) -> List[int]:
519+
# Write your code here.
520+
i,j=0,0
521+
directions=[(0,1),(1,0),(0,-1),(-1,0)]
522+
directionIndex=0
523+
m,n=len(matrix),len(matrix[0])
524+
arr=[n,m]
525+
res=[]
526+
while all(arr):
527+
for r in range(2):
528+
for c in range(arr[r]):
529+
res.append(matrix[i][j])
530+
if c<arr[r]-1:
531+
i,j=i+directions[directionIndex][0],j+directions[directionIndex][1]
532+
else:
533+
directionIndex=(directionIndex+1)%4
534+
i,j=i+directions[directionIndex][0],j+directions[directionIndex][1]
535+
arr[(r+1)%2]-=1
536+
return res
537+
```
538+
539+
### Day 19: Zero Matrix
540+
541+
You are given a matrix 'MATRIX' of dimension 'N' x 'M'. Your task is to make all the elements of row 'i' and column 'j' equal to 0 if any element in the ith row or jth column of the matrix is 0.
542+
543+
Note:
544+
545+
1. The number of rows should be at least 1.
546+
547+
2. The number of columns should be at least 1.
548+
549+
```py
550+
def zeroMatrix(matrix, n, m):
551+
# Write your code here.
552+
if n<=1 or m<=1:
553+
return matrix
554+
555+
first_row_have_zero=any(matrix[0][j]==0 for j in range(m))
556+
first_col_have_zero=any(matrix[i][0]==0 for i in range(n))
557+
558+
for i in range(1,n):
559+
for j in range(1,m):
560+
if matrix[i][j]==0:
561+
matrix[0][j]=0
562+
matrix[i][0]=0
563+
for i in range(1,n):
564+
for j in range(1,m):
565+
if matrix[0][j]==0 or matrix[i][0]==0:
566+
matrix[i][j]=0
567+
if first_row_have_zero:
568+
for j in range(m):
569+
matrix[0][j]=0
570+
if first_col_have_zero:
571+
for i in range(n):
572+
matrix[i][0]=0
573+
```
574+
575+
### Day 20: Pascal's Triangle
576+
577+
You are given an integer N. Your task is to return a 2-D ArrayList containing the pascal’s triangle till the row N.
578+
579+
A Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows. Pascal's triangle contains the values of the binomial coefficient.
580+
581+
```py
582+
def printPascal(n:int):
583+
# Write your code here.
584+
# Return a list of lists.
585+
res=[[1]]
586+
for i in range(1,n):
587+
res.append([1]+[res[i-1][j]+res[i-1][j-1] for j in range(1,i)]+[1])
588+
return res
589+
```
590+
591+
### Day 21: Number of Inversions
592+
593+
There is an integer array ‘A’ of size ‘N’.
594+
595+
Number of inversions in an array can be defined as the number of pairs of ‘i’, ‘j’ such that ‘i’ < ‘j’ and ‘A[i]’ > ‘A[j]’.
596+
597+
You must return the number of inversions in the array.
598+
599+
For example,
600+
Input:
601+
A = [5, 3, 2, 1, 4], N = 5
602+
Output:
603+
7
604+
Explanation:
605+
The pairs satisfying the condition for inversion are (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), and (3, 4).
606+
The number of inversions in the array is 7.
607+
608+
```py
609+
from typing import *
610+
611+
class Solution:
612+
def mergeSort(self,arr,low,high):
613+
count_inversion=0
614+
if low<high:
615+
mid=(low+high)//2
616+
count_inversion+=self.mergeSort(arr,low,mid)
617+
count_inversion+=self.mergeSort(arr,mid+1,high)
618+
count_inversion+=self.merge(arr,low,mid,high)
619+
return count_inversion
620+
def merge(self,arr,low,mid,high):
621+
count_inversion=0
622+
temp=[0]*(high-low+1)
623+
i=low
624+
j=mid+1
625+
k=0
626+
while i<=mid and j<=high:
627+
if arr[i]<=arr[j]:
628+
temp[k]=arr[i]
629+
k+=1
630+
i+=1
631+
else:
632+
temp[k]=arr[j]
633+
k+=1
634+
j+=1
635+
count_inversion+=mid-i+1 #se agrega la inversion
636+
while i<=mid:
637+
temp[k]=arr[i]
638+
k+=1
639+
i+=1
640+
while j<=high:
641+
temp[k]=arr[j]
642+
k+=1
643+
j+=1
644+
for i in range(low,high+1):
645+
arr[i]=temp[i-low]
646+
return count_inversion
647+
def numberOfInversions(a : List[int], n : int) -> int:
648+
# Write your code here.
649+
return Solution().mergeSort(a,0,n-1)
650+
```

certificados/certificado_level3.webp

21.7 KB
Binary file not shown.

day16/Valid Parentheses.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def isValidParenthesis(s: str) -> bool:
2+
# Write your code here
3+
stack=[]
4+
for c in s:
5+
if c=='(' or c=='[' or c=='{':
6+
stack.append(c)
7+
elif c==')':
8+
if stack and stack[-1]=='(':
9+
stack.pop()
10+
else:
11+
return False
12+
elif c==']':
13+
if stack and stack[-1]=='[':
14+
stack.pop()
15+
else:
16+
return False
17+
elif c=='}':
18+
if stack and stack[-1]=='{':
19+
stack.pop()
20+
else:
21+
return False
22+
return not stack
23+
24+
s="[[}["
25+
print(isValidParenthesis(s))

day17/Implement Upper Bound.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
def upperBound(arr: List[int], x: int, n: int) -> int:
4+
# Write your code here.
5+
l,r=0,n-1
6+
while l<=r:
7+
mid=(l+r)//2
8+
if arr[mid]==x and (arr[mid+1]>x if mid<n-1 else True):
9+
return mid+1
10+
elif arr[mid]>x:
11+
r=mid-1
12+
else:
13+
l=mid+1
14+
return l
15+
16+
#arr=[1,5,5,7,7,9,10]
17+
#x=5
18+
# arr=[1,2,5,6,10]
19+
# x=10
20+
arr=[1,4,7,8,10]
21+
x=7
22+
n=len(arr)
23+
print(upperBound(arr,x,n))

0 commit comments

Comments
 (0)