diff --git a/challenges/heap-algorithm/README.md b/challenges/heap-algorithm/README.md index be40826..d42c990 100644 --- a/challenges/heap-algorithm/README.md +++ b/challenges/heap-algorithm/README.md @@ -6,6 +6,8 @@ Created by [@theoludwig](https://github.com/theoludwig) on 8 November 2021. Write a program that generates all possible unique permutations of a string. +The order of the generated permutations is important, see the example below. + ## Source - [Heap's Algorithm - Wikipedia](https://en.wikipedia.org/wiki/Heap%27s_algorithm) @@ -25,10 +27,10 @@ abc ```txt abc bac -cba -bca cab acb +bca +cba ``` See the `test` folder for examples of input/output. diff --git a/challenges/heap-algorithm/solutions/c/function/solution.c b/challenges/heap-algorithm/solutions/c/function/solution.c index 8d5bafb..dd98d7b 100644 --- a/challenges/heap-algorithm/solutions/c/function/solution.c +++ b/challenges/heap-algorithm/solutions/c/function/solution.c @@ -5,34 +5,21 @@ #include "input.h" -char *swap(char *string, size_t index_from, size_t index_target) { - size_t string_length = strlen(string); - char *result = malloc(sizeof(char *) * (string_length)); - for (size_t index = 0; index < string_length; index++) { - if (index == index_from) { - result[index] = string[index_target]; - } else if (index == index_target) { - result[index] = string[index_from]; - } else { - result[index] = string[index]; - } - } - return result; +void swap(char *string, size_t index_from, size_t index_target) { + char temporary = string[index_from]; + string[index_from] = string[index_target]; + string[index_target] = temporary; } -void heap_algorithm(unsigned long number_of_elements_to_operate, char *string) { +void heap_algorithm(char *string, size_t number_of_elements_to_operate) { if (number_of_elements_to_operate == 1) { printf("%s\n", string); } else { - heap_algorithm(number_of_elements_to_operate - 1, string); + heap_algorithm(string, number_of_elements_to_operate - 1); for (size_t index = 0; index < number_of_elements_to_operate - 1; index++) { bool is_even = number_of_elements_to_operate % 2 == 0; - if (!is_even) { - string = swap(string, index, number_of_elements_to_operate - 1); - } else { - string = swap(string, 0, number_of_elements_to_operate - 1); - } - heap_algorithm(number_of_elements_to_operate - 1, string); + swap(string, is_even ? index : 0, number_of_elements_to_operate - 1); + heap_algorithm(string, number_of_elements_to_operate - 1); } } } @@ -40,6 +27,7 @@ void heap_algorithm(unsigned long number_of_elements_to_operate, char *string) { int main() { char *string = input(); size_t string_length = strlen(string); - heap_algorithm(string_length, string); + heap_algorithm(string, string_length); + free(string); return EXIT_SUCCESS; } diff --git a/challenges/heap-algorithm/test/1/output.txt b/challenges/heap-algorithm/test/1/output.txt index 7895438..a8b686a 100644 --- a/challenges/heap-algorithm/test/1/output.txt +++ b/challenges/heap-algorithm/test/1/output.txt @@ -1,6 +1,6 @@ abc bac -cba -bca cab -acb \ No newline at end of file +acb +bca +cba \ No newline at end of file diff --git a/challenges/heap-algorithm/test/2/output.txt b/challenges/heap-algorithm/test/2/output.txt index 4620edf..174b080 100644 --- a/challenges/heap-algorithm/test/2/output.txt +++ b/challenges/heap-algorithm/test/2/output.txt @@ -1,24 +1,24 @@ cool ocol -oocl -oocl ocol cool +oocl +oocl +loco +olco +cloo +lcoo +oclo +colo +colo +oclo +lcoo +cloo +olco +loco looc oloc -oolc -oolc oloc looc -cool -ocol -oocl -oocl -ocol -cool -looc -oloc oolc -oolc -oloc -looc \ No newline at end of file +oolc \ No newline at end of file diff --git a/challenges/heap-algorithm/test/3/output.txt b/challenges/heap-algorithm/test/3/output.txt index dcc6a8b..7310627 100644 --- a/challenges/heap-algorithm/test/3/output.txt +++ b/challenges/heap-algorithm/test/3/output.txt @@ -1,120 +1,120 @@ kayak akyak -yakak -aykak ykaak kyaak -aaykk -aaykk -yaakk -ayakk -yaakk -ayakk -kayak +aykak +yakak +aakyk +aakyk +kaayk +akayk +akayk +kaayk +kyaak +ykaak akyak -yakak -aykak -ykaak -kyaak -aaykk -aaykk -yaakk -ayakk -yaakk -ayakk kayak -akyak yakak aykak -ykaak -kyaak +ayakk +yaakk aaykk aaykk yaakk ayakk -yaakk -ayakk -kayak -akyak -yakak -aykak -ykaak -kyaak -aaykk -aaykk -yaakk -ayakk -yaakk -ayakk -kkyaa -kkyaa -ykkaa -kykaa -ykkaa -kykaa +kyaka +ykaka akyka kayka -ykaka -kyaka yakka aykka +kykaa +ykkaa kkyaa kkyaa ykkaa kykaa +kakya +akkya +kkaya +kkaya +akkya +kakya +kayka +akyka +ykaka +kyaka +aykka +yakka +aakky +aakky +kaaky +akaky +akaky +kaaky +kaaky +akaky +akaky +kaaky +aakky +aakky +akkay +kakay +kakay +akkay +kkaay +kkaay +kkaay +kkaay +akkay +kakay +kakay +akkay ykkaa kykaa +kykaa +ykkaa +kkyaa +kkyaa akyka kayka -ykaka -kyaka yakka aykka -kkaay -kkaay -akkay -kakay -akkay -kakay -akaky -kaaky -akaky -kaaky -aakky -aakky -kkaay -kkaay -akkay -kakay -akkay -kakay -akaky -kaaky -akaky -kaaky -aakky -aakky -kkaya -kkaya -akkya -kakya -akkya -kakya +kyaka +ykaka ykaka kyaka -akyka -kayka aykka yakka -kkaya -kkaya -akkya -kakya -akkya -kakya -ykaka -kyaka -akyka kayka -aykka -yakka \ No newline at end of file +akyka +akkya +kakya +kakya +akkya +kkaya +kkaya +akayk +kaayk +aakyk +aakyk +kaayk +akayk +ykaak +kyaak +aykak +yakak +kayak +akyak +aaykk +aaykk +yaakk +ayakk +ayakk +yaakk +yakak +aykak +kyaak +ykaak +akyak +kayak \ No newline at end of file diff --git a/challenges/heap-algorithm/test/4/output.txt b/challenges/heap-algorithm/test/4/output.txt index d23ac2a..ec2f6fa 100644 --- a/challenges/heap-algorithm/test/4/output.txt +++ b/challenges/heap-algorithm/test/4/output.txt @@ -1,720 +1,720 @@ azerty zaerty -ezarty -zearty eazrty aezrty -rzeaty -zreaty -ezraty -zeraty -erzaty -rezaty -azerty -zaerty -ezarty zearty -eazrty -aezrty -rzeaty +ezarty +rzaety +zraety +arzety +razety +zarety +azrety +aerzty +earzty +raezty +arezty +erazty +reazty +rezaty +erzaty zreaty +rzeaty ezraty zeraty -erzaty -rezaty -tzeray -zteray -eztray -zetray -etzray -tezray -rzetay -zretay -ezrtay -zertay -erztay -reztay -tzeray -zteray -eztray -zetray -etzray -tezray -rzetay -zretay -ezrtay -zertay -erztay -reztay +terazy +etrazy +rteazy +treazy +ertazy +retazy +aetrzy +eatrzy taerzy aterzy -eatrzy -aetrzy etarzy tearzy -raetzy +traezy +rtaezy +atrezy +tarezy +ratezy +artezy aretzy +raetzy eartzy aertzy -eratzy reatzy -taerzy -aterzy -eatrzy -aetrzy -etarzy -tearzy -raetzy -aretzy -eartzy -aertzy eratzy -reatzy -tazrey -atzrey -zatrey -aztrey -ztarey -tzarey -raztey -arztey -zartey -azrtey zratey rzatey -tazrey -atzrey -zatrey -aztrey -ztarey -tzarey +azrtey +zartey raztey arztey -zartey -azrtey -zratey -rzatey -tazery -atzery -zatery -aztery -ztaery -tzaery -eaztry -aeztry +trzaey +rtzaey +ztraey +tzraey +rztaey +zrtaey +zatrey +aztrey +tzarey +ztarey +atzrey +tazrey +tarzey +atrzey +rtazey +trazey +artzey +ratzey +eatzry +aetzry +teazry +etazry +atezry +taezry zaetry azetry -zeatry ezatry -tazery -atzery -zatery -aztery +zeatry +aeztry +eaztry +etzary +tezary +zetary +eztary +tzeary +zteary ztaery tzaery -eaztry -aeztry -zaetry -azetry -zeatry -ezatry -yzerta -zyerta -ezyrta -zeyrta -eyzrta -yezrta -rzeyta -zreyta -ezryta -zeryta -erzyta -rezyta -yzerta -zyerta -ezyrta -zeyrta -eyzrta -yezrta -rzeyta -zreyta -ezryta -zeryta -erzyta -rezyta -tzerya -zterya -eztrya -zetrya -etzrya -tezrya -rzetya -zretya -ezrtya -zertya -erztya -reztya -tzerya -zterya -eztrya -zetrya -etzrya -tezrya -rzetya -zretya -ezrtya -zertya -erztya -reztya -tyerza -yterza -eytrza -yetrza -etyrza -teyrza -ryetza -yretza -eyrtza -yertza -erytza -reytza -tyerza -yterza -eytrza -yetrza -etyrza -teyrza -ryetza -yretza -eyrtza -yertza -erytza -reytza -tyzrea -ytzrea -zytrea -yztrea -ztyrea -tzyrea -ryztea -yrztea -zyrtea -yzrtea -zrytea -rzytea -tyzrea -ytzrea -zytrea -yztrea -ztyrea -tzyrea -ryztea -yrztea -zyrtea -yzrtea -zrytea -rzytea -tyzera +aztery +zatery +tazery +atzery +rtzeay +trzeay +zrteay +rzteay +tzreay +ztreay +etrzay +terzay +retzay +ertzay +trezay +rtezay +rzetay +zretay +erztay +reztay +zertay +ezrtay +eztray +zetray +tezray +etzray +zteray +tzeray +yzerat +zyerat +eyzrat +yezrat +zeyrat +ezyrat +rzyeat +zryeat +yrzeat +ryzeat +zyreat +yzreat +yerzat +eyrzat +ryezat +yrezat +eryzat +reyzat +rezyat +erzyat +zreyat +rzeyat +ezryat +zeryat +aeryzt +earyzt +raeyzt +areyzt +erayzt +reayzt +yearzt +eyarzt +ayerzt +yaerzt +eayrzt +aeyrzt +aryezt +rayezt +yarezt +ayrezt +ryaezt +yraezt +yreazt +ryeazt +eyrazt +yerazt +reyazt +eryazt +zryaet +rzyaet +yzraet +zyraet +ryzaet +yrzaet +arzyet +razyet +zaryet +azryet +rzayet +zrayet +zyaret +yzaret +azyret +zayret +yazret +ayzret +ayrzet +yarzet +rayzet +aryzet +yrazet +ryazet +eyazrt +yeazrt +aeyzrt +eayzrt +yaezrt +ayezrt +zyeart +yzeart +ezyart +zeyart +yezart +eyzart +eazyrt +aezyrt +zeayrt +ezayrt +azeyrt +zaeyrt +zayert +azyert +yzaert +zyaert +ayzert +yazert +razeyt +arzeyt +zraeyt +rzaeyt +azreyt +zareyt +earzyt +aerzyt +reazyt +erazyt +arezyt +raezyt +rzeayt +zreayt +erzayt +rezayt +zerayt +ezrayt +ezaryt +zearyt +aezryt +eazryt +zaeryt +azeryt +ateryz +taeryz +eatryz +aetryz +tearyz +etaryz +rtaeyz +traeyz +arteyz +rateyz +tareyz +atreyz +aertyz +eartyz +raetyz +aretyz +eratyz +reatyz +retayz +ertayz +treayz +rteayz +etrayz +terayz +yeratz +eyratz +ryeatz +yreatz +eryatz +reyatz +aeyrtz +eayrtz +yaertz +ayertz +eyartz +yeartz +yraetz +ryaetz +ayretz +yaretz +rayetz +aryetz +areytz +raeytz +earytz +aerytz +reaytz +eraytz +trayez +rtayez +atryez +taryez +ratyez +artyez +yrtaez +rytaez +tyraez +ytraez +rtyaez +tryaez +tayrez +atyrez +ytarez +tyarez +aytrez +yatrez +yartez +ayrtez +ryatez +yratez +arytez +raytez +eaytrz +aeytrz +yeatrz +eyatrz +ayetrz +yaetrz +taeyrz +ateyrz +etayrz +teayrz +aetyrz +eatyrz +eytarz +yetarz +teyarz +etyarz +ytearz +tyearz +tyaerz +ytaerz +atyerz +tayerz +yaterz +ayterz +ryteaz +yrteaz +tryeaz +rtyeaz +ytreaz +tyreaz +eyrtaz +yertaz +reytaz +erytaz +yretaz +ryetaz +rteyaz +treyaz +ertyaz +retyaz +teryaz +etryaz +etyraz +teyraz +yetraz +eytraz +tyeraz +yteraz +ytzrae +tyzrae +zytrae +yztrae +tzyrae +ztyrae +rtyzae +tryzae +yrtzae +rytzae +tyrzae +ytrzae +yzrtae +zyrtae +ryztae +yrztae +zrytae +rzytae +rztyae +zrtyae +trzyae +rtzyae +ztryae +tzryae +azryte +zaryte +razyte +arzyte +zrayte +rzayte +yzarte +zyarte +ayzrte +yazrte +zayrte +azyrte +aryzte +rayzte +yarzte +ayrzte +ryazte +yrazte +yrzate +ryzate +zyrate +yzrate +rzyate +zryate +tryaze +rtyaze +ytraze +tyraze +rytaze +yrtaze +artyze +ratyze +taryze +atryze +rtayze +trayze +tyarze +ytarze +atyrze +tayrze +yatrze +aytrze +ayrtze +yartze +raytze +arytze +yratze +ryatze +zyatre +yzatre +azytre +zaytre +yaztre +ayztre +tyzare +ytzare +ztyare +tzyare +yztare +zytare +zatyre +aztyre +tzayre +ztayre +atzyre +tazyre +tayzre +atyzre +ytazre +tyazre +aytzre +yatzre +ratzye +artzye +trazye +rtazye +atrzye +tarzye +zartye +azrtye +rzatye +zratye +arztye +raztye +rtzaye +trzaye +zrtaye +rztaye +tzraye +ztraye +ztarye +tzarye +aztrye +zatrye +tazrye +atzrye +atzeyr +tazeyr +zateyr +azteyr +tzaeyr +ztaeyr +etazyr +teazyr +aetzyr +eatzyr +taezyr +atezyr +azetyr +zaetyr +eaztyr +aeztyr +zeatyr +ezatyr +eztayr +zetayr +tezayr +etzayr +zteayr +tzeayr +yzeatr +zyeatr +eyzatr +yezatr +zeyatr +ezyatr +azyetr +zayetr +yazetr +ayzetr +zyaetr +yzaetr +yeaztr +eyaztr +ayeztr +yaeztr +eayztr +aeyztr +aezytr +eazytr +zaeytr +azeytr +ezaytr +zeaytr +teayzr +etayzr +ateyzr +taeyzr +eatyzr +aetyzr +yetazr +eytazr +tyeazr +yteazr +etyazr +teyazr +tayezr +atyezr +ytaezr +tyaezr +aytezr +yatezr +yaetzr +ayetzr +eyatzr +yeatzr +aeytzr +eaytzr +zayter +azyter +yzater +zyater +ayzter +yazter +tazyer +atzyer +ztayer +tzayer +aztyer +zatyer +zytaer +yztaer +tzyaer +ztyaer +ytzaer +tyzaer +tyazer +ytazer +atyzer +tayzer +yatzer +aytzer +eytzar +yetzar +teyzar +etyzar +ytezar +tyezar +zyetar +yzetar +ezytar +zeytar +yeztar +eyztar +etzyar +tezyar +zetyar +eztyar +tzeyar +zteyar +ztyear +tzyear +yztear +zytear +tyzear +ytzear ytzera +tyzera zytera yztera -ztyera tzyera +ztyera +etyzra +teyzra +yetzra +eytzra +tyezra +ytezra +yzetra +zyetra eyztra yeztra -zyetra -yzetra zeytra ezytra -tyzera -ytzera -zytera -yztera -ztyera -tzyera -eyztra -yeztra -zyetra -yzetra -zeytra -ezytra -azerty -zaerty -ezarty -zearty -eazrty -aezrty -rzeaty -zreaty -ezraty -zeraty -erzaty -rezaty -azerty -zaerty -ezarty -zearty -eazrty -aezrty -rzeaty -zreaty -ezraty -zeraty -erzaty -rezaty -tzeray -zteray -eztray -zetray -etzray -tezray -rzetay -zretay -ezrtay -zertay -erztay -reztay -tzeray -zteray -eztray -zetray -etzray -tezray -rzetay -zretay -ezrtay -zertay -erztay -reztay -taerzy -aterzy -eatrzy -aetrzy -etarzy -tearzy -raetzy -aretzy -eartzy -aertzy -eratzy -reatzy -taerzy -aterzy -eatrzy -aetrzy -etarzy -tearzy -raetzy -aretzy -eartzy -aertzy -eratzy -reatzy -tazrey -atzrey -zatrey -aztrey -ztarey -tzarey -raztey -arztey -zartey -azrtey -zratey -rzatey -tazrey -atzrey -zatrey -aztrey -ztarey -tzarey -raztey -arztey -zartey -azrtey -zratey -rzatey -tazery -atzery -zatery -aztery -ztaery -tzaery -eaztry -aeztry -zaetry -azetry -zeatry -ezatry -tazery -atzery -zatery -aztery -ztaery -tzaery -eaztry -aeztry -zaetry -azetry -zeatry -ezatry -yzerta -zyerta -ezyrta -zeyrta -eyzrta -yezrta +eztyra +zetyra +tezyra +etzyra +zteyra +tzeyra rzeyta zreyta -ezryta -zeryta erzyta rezyta -yzerta +zeryta +ezryta +yzreta +zyreta +ryzeta +yrzeta +zryeta +rzyeta +reyzta +eryzta +yrezta +ryezta +eyrzta +yerzta +yezrta +eyzrta zyerta +yzerta ezyrta zeyrta -eyzrta -yezrta -rzeyta -zreyta -ezryta -zeryta -erzyta -rezyta -tzerya -zterya -eztrya -zetrya -etzrya -tezrya -rzetya -zretya -ezrtya -zertya -erztya -reztya -tzerya -zterya -eztrya -zetrya -etzrya -tezrya -rzetya -zretya -ezrtya -zertya -erztya -reztya -tyerza +teyrza +etyrza yterza +tyerza eytrza yetrza -etyrza -teyrza +retyza +ertyza +treyza +rteyza +etryza +teryza +tyreza +ytreza +rtyeza +tryeza +yrteza +ryteza ryetza yretza -eyrtza -yertza erytza reytza -tyerza -yterza -eytrza -yetrza -etyrza -teyrza -ryetza -yretza -eyrtza yertza -erytza -reytza -tyzrea -ytzrea -zytrea -yztrea -ztyrea -tzyrea -ryztea -yrztea +eyrtza zyrtea yzrtea -zrytea rzytea +zrytea +yrztea +ryztea tyzrea ytzrea -zytrea -yztrea ztyrea tzyrea -ryztea -yrztea -zyrtea -yzrtea -zrytea -rzytea -tyzera -ytzera -zytera -yztera -ztyera -tzyera -eyztra -yeztra -zyetra -yzetra -zeytra -ezytra -tyzera -ytzera -zytera -yztera -ztyera -tzyera -eyztra -yeztra -zyetra -yzetra -zeytra -ezytra -azerty -zaerty -ezarty -zearty -eazrty -aezrty -rzeaty -zreaty -ezraty -zeraty -erzaty -rezaty -azerty -zaerty -ezarty -zearty -eazrty -aezrty -rzeaty -zreaty -ezraty -zeraty -erzaty -rezaty -tzeray -zteray -eztray -zetray -etzray -tezray -rzetay -zretay -ezrtay -zertay -erztay -reztay -tzeray -zteray -eztray -zetray -etzray -tezray -rzetay -zretay -ezrtay -zertay -erztay -reztay -taerzy -aterzy -eatrzy -aetrzy -etarzy -tearzy -raetzy -aretzy -eartzy -aertzy -eratzy -reatzy -taerzy -aterzy -eatrzy -aetrzy -etarzy -tearzy -raetzy -aretzy -eartzy -aertzy -eratzy -reatzy -tazrey -atzrey -zatrey -aztrey -ztarey -tzarey -raztey -arztey -zartey -azrtey -zratey -rzatey -tazrey -atzrey -zatrey -aztrey -ztarey -tzarey -raztey -arztey -zartey -azrtey -zratey -rzatey -tazery -atzery -zatery -aztery -ztaery -tzaery -eaztry -aeztry -zaetry -azetry -zeatry -ezatry -tazery -atzery -zatery -aztery -ztaery -tzaery -eaztry -aeztry -zaetry -azetry -zeatry -ezatry -yzerta -zyerta -ezyrta -zeyrta -eyzrta -yezrta -rzeyta -zreyta -ezryta -zeryta -erzyta -rezyta -yzerta -zyerta -ezyrta -zeyrta -eyzrta -yezrta -rzeyta -zreyta -ezryta -zeryta -erzyta -rezyta -tzerya -zterya -eztrya -zetrya -etzrya -tezrya -rzetya +yztrea +zytrea +zrtyea +rztyea +tzryea +ztryea +rtzyea +trzyea +tryzea +rtyzea +ytrzea +tyrzea +rytzea +yrtzea +ertzya +retzya +terzya +etrzya +rtezya +trezya zretya +rzetya ezrtya zertya -erztya reztya -tzerya -zterya -eztrya -zetrya +erztya etzrya tezrya -rzetya -zretya -ezrtya -zertya -erztya -reztya -tyerza -yterza -eytrza -yetrza -etyrza -teyrza -ryetza -yretza -eyrtza -yertza -erytza -reytza -tyerza -yterza -eytrza -yetrza -etyrza -teyrza -ryetza -yretza -eyrtza -yertza -erytza -reytza -tyzrea -ytzrea -zytrea -yztrea -ztyrea -tzyrea -ryztea -yrztea -zyrtea -yzrtea -zrytea -rzytea -tyzrea -ytzrea -zytrea -yztrea -ztyrea -tzyrea -ryztea -yrztea -zyrtea -yzrtea -zrytea -rzytea -tyzera -ytzera -zytera -yztera -ztyera -tzyera -eyztra -yeztra -zyetra -yzetra -zeytra -ezytra -tyzera -ytzera -zytera -yztera -ztyera -tzyera -eyztra -yeztra -zyetra -yzetra -zeytra -ezytra \ No newline at end of file +zetrya +eztrya +tzerya +zterya +ztreya +tzreya +rzteya +zrteya +trzeya +rtzeya \ No newline at end of file diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/character.c b/challenges/look-and-say-sequence-conway/solutions/c/function/character.c index bd60265..3e8c0ff 100644 --- a/challenges/look-and-say-sequence-conway/solutions/c/function/character.c +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/character.c @@ -5,10 +5,3 @@ void character_append(char* string, char character) { string[length] = character; string[length + 1] = '\0'; } - -void character_append_many(char* string, char* characters) { - size_t characters_length = strlen(characters); - for (size_t index = 0; index < characters_length; index++) { - character_append(string, characters[index]); - } -} diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/character.h b/challenges/look-and-say-sequence-conway/solutions/c/function/character.h index 1535477..e3a2037 100644 --- a/challenges/look-and-say-sequence-conway/solutions/c/function/character.h +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/character.h @@ -13,13 +13,4 @@ */ void character_append(char* string, char character); -/** - * @brief Append many characters to a string, assuming string points to an array - * with enough space. - * - * @param string - * @param characters - */ -void character_append_many(char* string, char* characters); - #endif diff --git a/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c b/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c index 33bfb8a..85e19fb 100644 --- a/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c +++ b/challenges/look-and-say-sequence-conway/solutions/c/function/solution.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,22 +6,129 @@ #include "character.h" #include "input.h" +size_t string_get_length(const char* string) { + size_t length = 0; + while (string[length] != '\0') { + length++; + } + return length; +} + +char* string_copy(const char* string) { + size_t source_length = string_get_length(string); + char* copy = malloc(sizeof(char) * (source_length + 1)); + if (copy == NULL) { + perror("Error (string_copy)"); + exit(EXIT_FAILURE); + } + size_t index; + for (index = 0; index < source_length; index++) { + copy[index] = string[index]; + } + copy[index] = '\0'; + return copy; +} + +unsigned long long mathematics_absolute_value(const long long number) { + if (number >= 0) { + return number; + } + return -number; +} + +void string_reverse(char* string) { + size_t string_length = string_get_length(string); + size_t index_start = 0; + size_t index_end = string_length - 1; + while (index_start < index_end) { + char temporary = string[index_start]; + string[index_start] = string[index_end]; + string[index_end] = temporary; + index_start++; + index_end--; + } +} + +char* convert_character_to_string(const char character) { + char* string = malloc(sizeof(char) * 2); + if (string == NULL) { + perror("Error (convert_character_to_string)"); + exit(EXIT_FAILURE); + } + string[0] = character; + string[1] = '\0'; + return string; +} + +char convert_digit_to_character(const char digit) { + return digit + '0'; +} + +void string_concatenate(char** destination, char* source) { + size_t destination_length = string_get_length(*destination); + size_t source_length = string_get_length(source); + size_t new_length = destination_length + source_length; + *destination = realloc(*destination, sizeof(char) * (new_length + 1)); + if (*destination == NULL) { + perror("Error (string_concatenate)"); + exit(EXIT_FAILURE); + } + size_t index_destination = destination_length; + for (size_t index_source = 0; index_source < source_length; index_source++) { + (*destination)[index_destination++] = source[index_source]; + } + (*destination)[index_destination] = '\0'; +} + +char* convert_number_to_string(const long long integer) { + if (integer == 0) { + return convert_character_to_string('0'); + } + bool is_negative = integer < 0; + size_t length = 1; + long long current = mathematics_absolute_value(integer); + while (current != 0) { + current = current / 10; + length++; + } + if (is_negative) { + length++; + } + char* string = malloc(sizeof(char) * length); + if (string == NULL) { + perror("Error (convert_number_to_string)"); + exit(EXIT_FAILURE); + } + current = mathematics_absolute_value(integer); + size_t index = 0; + while (current != 0) { + string[index++] = convert_digit_to_character(current % 10); + current = current / 10; + } + if (is_negative) { + string[index++] = '-'; + } + string[index] = '\0'; + string_reverse(string); + return string; +} + int main() { char* string = input(); - size_t string_length = strlen(string); - char* result = malloc(sizeof(char) * (string_length + 1)); + size_t string_length = string_get_length(string); + char* result = string_copy(""); for (size_t index = 0; index < string_length; index++) { - unsigned char number_of_appearances = 0; + long long number_of_appearances = 0; char value_to_search = string[index]; - size_t iteration = index; - while (iteration < string_length && string[iteration] == value_to_search) { + while (number_of_appearances + index < string_length && string[number_of_appearances + index] == value_to_search) { number_of_appearances++; - iteration++; } - char* number_of_appearances_string = malloc(sizeof(char) * (string_length + 1)); - snprintf(number_of_appearances_string, sizeof(result), "%hhu", number_of_appearances); - character_append_many(result, number_of_appearances_string); - character_append(result, value_to_search); + char* number_of_appearances_string = convert_number_to_string(number_of_appearances); + string_concatenate(&result, number_of_appearances_string); + char* value_string = convert_character_to_string(value_to_search); + string_concatenate(&result, value_string); + free(number_of_appearances_string); + free(value_string); index += number_of_appearances - 1; } printf("%s\n", result); diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c index 54269aa..dad3ebb 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c @@ -13,48 +13,29 @@ void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_col } int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns) { - int **array = malloc(sizeof(int) * 2); - *number_of_rows = 1; - *number_of_columns = 1; - array[0] = malloc(*number_of_columns * sizeof(int)); - array[0][0] = 0; - char character; - size_t length = 1; - char *string = malloc(length * sizeof(char)); - *string = '\0'; - while ((character = getchar()) != EOF) { - if (character == '\n') { - int number = atoi(string); - array[*number_of_rows - 1][*number_of_columns - 1] = number; - length = 1; - memset(string, 0, length * sizeof(char)); - *string = '\0'; - *number_of_rows = *number_of_rows + 1; - *number_of_columns = 1; - array = realloc(array, *number_of_rows * sizeof(int *)); - array[*number_of_rows - 1] = malloc(*number_of_columns * sizeof(int)); - } else { - if (character == ' ') { - int number = atoi(string); - array[*number_of_rows - 1][*number_of_columns - 1] = number; - length = 1; - memset(string, 0, length * sizeof(char)); - *string = '\0'; - *number_of_columns = *number_of_columns + 1; - } else { - length++; - string = realloc(string, length * sizeof(char)); - character_append(string, character); - } + *number_of_rows = 0; + *number_of_columns = 0; + int **array = malloc(sizeof(int *)); + char *line = input(); + while (string_get_length(line) != 0) { + char **integers_string = string_split(line, ' ', number_of_columns); + array[*number_of_rows] = malloc(*number_of_columns * sizeof(int)); + for (size_t column = 0; column < *number_of_columns; column++) { + array[*number_of_rows][column] = atoi(integers_string[column]); + free(integers_string[column]); } + free(integers_string); + free(line); + line = input(); + *number_of_rows += 1; + array = realloc(array, (*number_of_rows + 1) * sizeof(int *)); } - int number = atoi(string); - array[*number_of_rows - 1][*number_of_columns - 1] = number; + free(line); return array; } int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *number_of_columns) { - int **rotated_array = malloc(*number_of_rows * sizeof(int)); + int **rotated_array = malloc(*number_of_rows * sizeof(int *)); for (size_t row = 0; row < *number_of_rows; row++) { rotated_array[row] = malloc(*number_of_columns * sizeof(int)); } diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h index 2b478c6..2b463a1 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h @@ -6,6 +6,8 @@ #include #include "character.h" +#include "input.h" +#include "string.h" /** * @brief Prints a 2D array of integers. diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c index db76433..62e9c91 100644 --- a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c @@ -9,6 +9,7 @@ int main() { size_t number_of_columns = 0; char *direction = input(); int **array_input = array_2D_int_input(&number_of_rows, &number_of_columns); + size_t initial_number_of_rows = number_of_rows; int **array; if (strcmp(direction, "clockwise") == 0) { @@ -19,6 +20,10 @@ int main() { array_2D_int_print(array, number_of_rows, number_of_columns); free(direction); + for (size_t row = 0; row < initial_number_of_rows; row++) { + free(array_input[row]); + } + free(array_input); for (size_t row = 0; row < number_of_rows; row++) { free(array[row]); } diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/string.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/string.c new file mode 100644 index 0000000..6a69e8b --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/string.c @@ -0,0 +1,64 @@ +#include "string.h" + +size_t string_get_length(const char* string) { + size_t length = 0; + while (string[length] != '\0') { + length++; + } + return length; +} + +char* string_copy(const char* string) { + size_t source_length = string_get_length(string); + char* copy = malloc(sizeof(char) * (source_length + 1)); + if (copy == NULL) { + perror("Error (string_copy)"); + exit(EXIT_FAILURE); + } + size_t index; + for (index = 0; index < source_length; index++) { + copy[index] = string[index]; + } + copy[index] = '\0'; + return copy; +} + +char** string_split(const char* string, char separator, size_t* result_size) { + size_t string_length = string_get_length(string); + size_t index_string = 0; + size_t index_current = 0; + size_t index_result = 0; + char* current = malloc(sizeof(char) * (string_length + 1)); + char** result = NULL; + if (current == NULL) { + perror("Error (string_split)"); + exit(EXIT_FAILURE); + } + while (index_string < string_length) { + if (string[index_string] == separator) { + current[index_current] = '\0'; + result = realloc(result, sizeof(char*) * (index_result + 1)); + if (result == NULL) { + perror("Error (string_split)"); + exit(EXIT_FAILURE); + } + result[index_result] = string_copy(current); + index_result++; + index_current = 0; + } else { + current[index_current] = string[index_string]; + index_current++; + } + index_string++; + } + current[index_current] = '\0'; + result = realloc(result, sizeof(char*) * (index_result + 1)); + if (result == NULL) { + perror("Error (string_split)"); + exit(EXIT_FAILURE); + } + result[index_result] = string_copy(current); + free(current); + *result_size = index_result + 1; + return result; +} diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/string.h b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/string.h new file mode 100644 index 0000000..fdf98b5 --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/string.h @@ -0,0 +1,35 @@ +#ifndef __STRING__ +#define __STRING__ + +#include +#include +#include +#include + +/** + * @brief Return the length of a string (excluding '\0'). + * + * @param string + * @return size_t + */ +size_t string_get_length(const char* string); + +/** + * @brief Return the copy of a string. + * + * @param string + * @return string_t + */ +char* string_copy(const char* string); + +/** + * @brief Split a string into substrings using the specified separator and return them as an array and update the pointer `result_size` to the resulting size of the created array. + * + * @param string + * @param separator + * @param result_size + * @return string_t* + */ +char** string_split(const char* string, char separator, size_t* result_size); + +#endif