
Rapidkode: faites-les bien la première fois
RapidKode est un package Python qui fournit des structures de données rapides, flexibles et expressives, des alogorithmes conçus pour rendre le travail avec une programmation compétitive et un codage facile et intuitif. Il vise à être le bloc de construction fondamental de haut niveau pour une programmation compétitive dans Python.Te RapidKode, vous pouvez effectuer des algorithmes complexes en moins de temps, tous les algorithmes sont optimisés pour leurs meilleurs pour réduire la complexité du temps pour vous démarquer dans le classement. Il n'y a plus de temps à gaspiller d'énormes délais de code et de les déboguer plus tard, avec RapidKode, tout se passe à portée de main avec une seule ligne de code. L'objectif de Rapidkode est d'aider les débutants à démarrer dans la programmation compétente, à comprendre l'importance du temps et de l'espace. La devise de faire de RapidKode est de «bien faire les choses la première fois» au lieu de dépenser 10 minutes précieuses sur les fonctions utilles.
Installation:
ou
Pour les problèmes, les rapports de bogues et les contributions, visitez le dépôt de développement -> Cliquez ici
Fonctions disponibles:
Fonctions du nombre:
| syntaxe | opération |
|---|
| nombres.gen_sparsenum_upto (x) | génère un numéro clairsemé jusqu'à la plage donnée |
| nombres.get_sparsenum_after (n) | Renvoie le numéro clairsemé réussi pour le numéro donné |
| nombres.Checkprime (x) | Renvoie True si le numéro est Prime |
| nombres.getprimes.generate (x) | Renvoie les premiers nombres premiers |
| nombres.getprimes.upto (x) | Renvoie des nombres premiers à la plage donnée |
| nombres.getprimes.inrange (x, y) | Renvoie des nombres premiers dans la plage donnée |
| nombres.fib.getElement (x) | Renvoie le numéro de Fibonacci X'th |
| nombres.fib.generate (x) | Renvoie les premiers numéros X Fibonacci |
Exemple:
import rapidkode as rk
var = rk . numbers . gen_sparsenum_upto ( 100 )
print ( var )
var = rk . numbers . get_sparsenum_after ( 3289 )
print ( var )
var = rk . numbers . checkprime ( 8364 )
print ( var )
var = rk . numbers . getprimes . generate ( 100 )
print ( var )
var = rk . numbers . getprimes . inrange ( 100 , 500 )
print ( var )
var = rk . numbers . fib . getelement ( 58 )
print ( var )
var = rk . numbers . fib . generate ( 25 )
print ( var )
Sortir:
[0, 1, 2, 4, 5, 8, 9, 10, 16, 17, 18, 20, 21, 32, 33, 34, 36, 37, 40, 41, 42, 64, 65, 66, 68, 69, 72, 73, 74, 80, 81, 82, 84, 85, 128]
4096
False
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]
[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499]
365435296162
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
Système numérique - Fonctions de conversion:
| Syntaxe | Opération |
|---|
| converti (x, 'sys'). en ('new_sys') | Convertit x de sys au nouveau système |
| Exemple: | |
| convertir (9845, «déc»). en («bin») | Convertit 9845 de décimal à binaire |
| convertir (3745, «oct»). en («hex») | Convertit 3745 de l'octal à l'hexadécimal |
- Vous pouvez remplacer par
sys par ['bin', 'dec', 'oct', 'hex'] et new_sys par ['bin', 'dec', 'oct', 'hex'], pour effectuer des conversions de nombres.
import rapidkode as rk
converted_num_1 = rk . convert ( 2013 , 'dec' ). to ( 'bin' )
print ( converted_num_1 )
converted_num_2 = rk . convert ( 11111011101 , 'bin' ). to ( 'hex' )
print ( converted_num_2 )
converted_num_3 = rk . convert ( '7dd' , 'hex' ). to ( 'dec' )
print ( converted_num_3 )
converted_num_4 = rk . convert ( 5634 , 'oct' ). to ( 'dec' )
print ( converted_num_4 )
converted_num_5 = rk . convert ( 2972 , 'hex' ). to ( 'oct' )
print ( converted_num_5 )
converted_num_6 = rk . convert ( 24562 , 'oct' ). to ( 'bin' )
print ( converted_num_6 )
Sortir:
11111011101
7dd
2013
2972
24562
10100101110010
Algorithmes de recherche:
| Technique | Syntaxe | Opération | Complexité temporelle |
|---|
| Recherche linéaire | linear.search (arr, x) | Renvoie la position de x dans arr | Sur) |
| Recherche binaire | Binary.Search (arr, x) | Renvoie la position de x dans arr | O (log n) |
| Recherche de saut | Jump.search (arr, x) | Renvoie la position de x dans arr | O (√ n) |
| Recherche d'interpolation | interpolation.search (arr, x) | Renvoie la position de x dans arr | O (log2 (log2 n)) |
| Recherche exponentielle | Exponentiale.Search (Arr, X) | Renvoie la position de x dans arr | O (log2 i) |
| Recherche ternaire | ternary.search (arr, x) | Renvoie la position de x dans arr | O (log3 n) |
De plus que vous pouvez utiliser:
| Fonction | Opération |
|---|
| .montrer() | imprime le code dans le terminal |
| .info () | Donne la brève information |
| .algo () | Imprime l'algorithme sage étape |
Exemple:
import rapidkode as rk
> >> rk . binary . show ()
> >> rk . binary . info ()
> >> rk . binary . algo ()
Sortir:
def binarysearch ( arr , x ):
l = 0
r = len ( arr ) - 1
while l <= r :
mid = l + ( r - l ) // 2
if arr [ mid ] == x :
return mid
elif arr [ mid ] < x :
l = mid + 1
else :
r = mid - 1
return "element not found"
Binary search is the search technique that works efficiently on sorted lists
. Hence, to search an element into some list using the binary search technique, we must ensure that the list is sorted
. Binary search follows the divide and conquer approach in which the list is divided into two halves, and the item is compared with the middle element of the list
. If the match is found then, the location of the middle element is returned
. Otherwise, we search into either of the halves depending upon the result produced through the match
Algorithm
Step 1 - Read the search element from the user.
Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list.
Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function.
Step 5 - If both are not matched, then check whether the search element is smaller or larger than the middle element.
Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.
Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right sublist of the middle element.
Step 8 - Repeat the same process until we find the search element in the list or until sublist contains only one element.
Step 9 - If that element also doesn't match with the search element, then display "Element is not found in the list!!!" and terminate the function.
Les trois fonctions .show() , .info() , .algo() peuvent être utilisées pour les 6 techniques de recherche.
Algorithmes de tri:
| Technique | Syntaxe | Opération | Complexité temporelle |
|---|
| Tri de sélection | SELECTION.SORT (ARR) | Trie et renvoie le tableau donné | O (n ^ 2) |
| Tri bulle | bubble.sort (arr) | Trie et renvoie le tableau donné | O (n ^ 2) |
| Tri insertion | insertion.sort (arr) | Trie et renvoie le tableau donné | O (n ^ 2) |
| Fusion | Merge.sort (arr) | Trie et renvoie le tableau donné | O (n log (n)) |
| Trie de tas | Heap.Sort (Arr) | Trie et renvoie le tableau donné | O (n log (n)) |
| Tri rapide | Quick.sort (démarrage, fin, arr) | Trie et renvoie le tableau donné | O (n ^ 2) |
| Compter le tri | count.sort (arr) | Trie et renvoie le tableau donné | O (n + k) |
| Radix Toi | radix.sort (arr) | Trie et renvoie le tableau donné | O (NK) |
| Trier | Bucket.Sort (ARR) | Trie et renvoie le tableau donné | O (n + k) |
| Tri | shell.sort (arr) | Trie et renvoie le tableau donné | O (nlog n) |
| Peigne | peigne.Sort (arr) | Trie et renvoie le tableau donné | O (n log n) |
| Tri | pigeonhole.sort (arr) | Trie et renvoie le tableau donné | O (n + n) |
| Toi de cycle | cycle.sort (arr) | Trie et renvoie le tableau donné | O (N2) |
De plus que vous pouvez utiliser:
| Fonction | Opération |
|---|
| .montrer() | imprime le code dans le terminal |
| .info () | Donne la brève information |
| .algo () | Imprime l'algorithme sage étape |
Exemple:
import rapidkode as rk
> >> rk . count . show ()
> >> rk . count . info ()
> >> rk . count . algo ()
Sortir:
def countsort ( arr ):
output = [ 0 for i in range ( len ( arr ))]
count = [ 0 for i in range ( 256 )]
array = [ 0 for _ in arr ]
for i in arr :
count [ i ] += 1
for i in range ( 256 ):
count [ i ] += count [ i - 1 ]
for i in range ( len ( arr )):
output [ count [ arr [ i ]] - 1 ] = arr [ i ]
count [ arr [ i ]] -= 1
for i in range ( len ( arr )):
array [ i ] = output [ i ]
return array Counting sort is a sorting algorithm that sorts the elements of an array by counting the number of occurrences of each unique element in the array
. The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array
. Counting sort is a sorting technique based on keys between a specific range
. It works by counting the number of objects having distinct key values (kind of hashing)
. Then doing some arithmetic to calculate the position of each object in the output sequence
Algorithm
step 1 - Find out the maximum element (let it be max) from the given array.
step 2 - Initialize an array of length max+1 with all elements 0.
This array is used for storing the count of the elements in the array.
step 3 - Store the count of each element at their respective index in count array
For example: if the count of element 3 is 2 then, 2 is stored in the 3rd position of count array.
If element "5" is not present in the array, then 0 is stored in 5th position.
step 4 - Store cumulative sum of the elements of the count array.
It helps in placing the elements into the correct index of the sorted array.
step 5 - Find the index of each element of the original array in the count array.
This gives the cumulative count. Place the element at the index calculated as shown in figure below.
step 6 - After placing each element at its correct position, decrease its count by one.
Les trois fonctions .show() , .info() , .algo() peuvent être utilisées pour les 13 techniques de tri.
Fonctions du graphique:
| Syntaxe | Opération |
|---|
| .buildge (u, v) | Créer un bord graphique |
| .BuildMultisedge ([]) | Crée un graphique avec la liste de coords donnée |
| .Bfs (x) | Effectuer une première recherche |
| .Dfs (x) | Effectue la première recherche en profondeur |
| .findap () | Renvoie le point d'articulation du graphique |
Exemple:
import rapidkode as rk
# make graph object with graph() class
my_graph = rk . graph ()
# adding one edge at a time
my_graph . buildedge ( 0 , 1 )
my_graph . buildedge ( 0 , 2 )
my_graph . buildedge ( 1 , 2 )
my_graph . buildedge ( 2 , 0 )
my_graph . buildedge ( 2 , 3 )
my_graph . buildedge ( 3 , 3 )
import rapidkode as rk
# make graph object with graph() class
my_graph = rk . graph ()
# adding multiple edges at once
my_graph . buildmultiedge ([ 0 , 1 , 0 , 2 , 1 , 2 , 2 , 0 , 2 , 3 , 3 , 3 ])
import rapidkode as rk
# make graph object with graph() class
my_graph = rk . graph ()
my_graph . buildmultiedge ([ 0 , 1 , 0 , 2 , 1 , 2 , 2 , 0 , 2 , 3 , 3 , 3 ])
# performing BFS from edge 2
print ( my_graph . BFS ( 2 ))
# performing DFS from edge 2
print ( my_graph . DFS ( 2 ))
# finding the Articulation Point
print ( my_graph . findAP ())
Sortir:
['-->', 2, '-->', 0, '-->', 3, '-->', 1]
['-->', 2, '-->', 0, '-->', 1, '-->', 3]
2
Fonctions du motif:
La fonction suivante utilise un algorithme Rabin-Karp qui est un algorithme utilisé pour la recherche / correspondance dans le texte à l'aide d'une fonction de hachage. Contrairement à l'algorithme de correspondance de chaîne naïf, il ne voyage pas à travers tous les personnages de la phase initiale, il filtre plutôt les caractères qui ne correspondent pas, puis effectue la comparaison.
| Syntaxe | Opération |
|---|
| Pattern. | Renvoie True si la chaîne A est présente dans la chaîne B |
| Pattern.Where Is (a) .inn (b) | Renvoie la position d'index de la chaîne A dans la chaîne B |
Exemple:
import rapidkode as rk
a = 'sasi'
b = 'satyasasivatsal'
print ( rk . isthere ( a ). inn ( b ))
print ( rk . whereis ( a ). inn ( b ))
Sortir:
Fonctions LinkedList:
| Opération | Syntaxe |
|---|
| .ins_beg (nœud) | Insère un nouveau nœud au début |
| .ins_end (nœud) | Insère un nouveau nœud à la fin |
| .ins_after (pos, nœud) | Insère un nouveau nœud après le nœud spécifié |
| .ins_before (pos, nœud) | Insère un nouveau nœud avant le nœud spécifié |
| .del_node (nœud) | Supprime le nœud spécifié |
| .return_as_list () | Renvoie LinkedList comme liste Python |
Exemple:
import rapidkode as rk
my_list = rk . linkedlist ()
my_list . head = rk . node ( 'a' )
s1 = rk . node ( 'b' )
s2 = rk . node ( 'c' )
s3 = rk . node ( 'd' )
s4 = rk . node ( 'e' )
s5 = rk . node ( 'f' )
s6 = rk . node ( 'g' )
my_list . head . next = s1
s1 . next = s2
s2 . next = s3
s3 . next = s4
s4 . next = s5
s5 . next = s6
print ( my_list )
Sortir :
a -> b -> c -> d -> e -> f -> g -> None
Exemple -2:
# insertion at beginning
my_list . ins_beg ( rk . node ( 'A' ))
# insertion at end
my_list . ins_end ( rk . node ( 'G' ))
# insertion at positiom
my_list . ins_after ( 'e' , rk . node ( 'E' ))
# insertion at position
my_list . ins_before ( 'c' , rk . node ( 'C' ))
# deletion of ndoe
my_list . del_node ( 'b' )
# returning as list
my_listt = my_list . return_as_list ()
print ( my_list )
print ( my_listt )
Sortir :
A -> a -> C -> c -> d -> e -> E -> f -> g -> G -> None
['A', 'a', 'C', 'c', 'd', 'e', 'E', 'f', 'g', 'G', 'None']
Fictions de manipulation de bits:
| Syntaxe | Opération |
|---|
| bits.toggle_bits (x) | Bascule les bits définis et les bits non définis |
| bits.convert_to_bin (x) | Convertit un nombre donné en binaire |
| bits.counsetbits (x) | Renvoie le numéro de bits définis dans un numéro de déc. |
| bits.rotate_byleft (x, d) | Fait pivoter les bits à gauche par D fois |
| bits.rotate_byright (x, d) | Fait pivoter les bits à gauche par D fois |
| bits.Countflips (x, y) | Renvoie le n ° de flips pour faire x comme y |
Exemple:
import rapidkode as rk
var = rk . bits . toggle_bits ( 873652 )
print ( var )
var = rk . bits . convert_to_bin ( 873652 )
print ( var )
var = rk . bits . countsetbits ( 873652 )
print ( var )
var = rk . bits . rotate_byleft ( 873652 , 4 )
print ( var )
var = rk . bits . rotate_byright ( 873652 , 4 )
print ( var )
var = rk . bits . countflips ( 8934756 , 873652 )
print ( var )
Sortir:
960632
11010101010010110100
8474306
13978432
54603
7
Autres fonctions MISC:
| Syntaxe | Opération |
|---|
| .showsieves () | Imprime le code de tamis pour trouver un nombre premier dans le terminal |
| getprimefactors.fornum (x) | Renvoie une liste de facteurs premiers pour le nombre donné |
| FindgcDof (x, y) | Renvoie GCD des numéros donnés |
| findinversions.for (arr) | Renvoie à quel point le tableau est proche d'être trié |
| catlan_numbers.getElement (x) | Renvoie le numéro Catlan X'th |
| catlan_numbers.gen (x) | Renvoie une liste des premiers x catlan_numbers |
Exemple:
import rapidkode as rk
var = rk . getprimefactors . fornum ( 6754 )
print ( var )
var = rk . findgcdof ( 97345435 , 8764897 )
print ( var )
var = rk . findinversions . forr ([ 1 , 20 , 6 , 4 , 5 ])
print ( var )
var = rk . catlan_numbers . getelement ( 15 )
print ( var )
var = rk . catlan_numbers . gen ( 28 )
print ( var )
Sortir:
[2, 11, 307.0]
1
5
9694845.0
[1.0, 1.0, 2.0, 5.0, 14.0, 42.0, 132.0, 429.0, 1430.0, 4862.0, 16796.0, 58786.0, 208012.0, 742900.0, 2674440.0, 9694845.0, 35357670.0, 129644790.0, 477638700.0, 1767263190.0, 6564120420.0, 24466267020.0, 91482563640.0, 343059613650.0, 1289904147324.0, 4861946401452.0, 18367353072152.0, 69533550916004.0]
Contribuant à Rapidkode
- Toutes les contributions, rapports de bogues, correctifs de bogues, améliorations de documentation, améliorations et idées sont les bienvenues.
- Soulever un problème si vous trouvez un problème
- Si vous aimez contribuer, fourchez le repo soulever un problème avant de faire une demande de traction, il sera facile de gérer
- Crédits de logo et d'en-tête -> M.Sri Harsha❤️
Joyeux Koding Rapid !!