From 248bea97babbe9a8657ec355bc3048347d3e6580 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Fri, 21 Dec 2018 22:25:11 +0800 Subject: [PATCH 01/72] Add @aaditkamat as a contributor --- .all-contributorsrc | 10 ++++++++++ CONTRIBUTORS.md | 4 ++-- Day1/Ruby/fizzbuzz.rb | 0 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Day1/Ruby/fizzbuzz.rb diff --git a/.all-contributorsrc b/.all-contributorsrc index 6d620ded..485e1629 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -101,6 +101,16 @@ "contributions": [ "code" ] + }, + { + "login": "aaditkamat", + "name": "Aadit Kamat", + "avatar_url": "https://avatars0.githubusercontent.com/u/30969577?v=4", + "profile": "https://sg.linkedin.com/in/aadit-kamat-12a5a8140", + "contributions": [ + "doc", + "code" + ] } ] } diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 2fbd5402..b3033a7a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,5 +1,5 @@ -[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors) ## Contributors Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): @@ -8,7 +8,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds | [
MADHAV BAHL](http://madhavbahl.tech/)
[πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=MadhavBahlMD "Code") [πŸ“](#blog-MadhavBahlMD "Blogposts") [πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=MadhavBahlMD "Documentation") [🎨](#design-MadhavBahlMD "Design") [βœ…](#tutorial-MadhavBahlMD "Tutorials") | [
Rajdeep Roy Chowdhury](http://www.linkedin.com/in/razdeeproychowdhury)
[πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Razdeep "Code") [πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Razdeep "Documentation") [βœ…](#tutorial-Razdeep "Tutorials") | [
Hrishi](https://hrishi1999.github.io)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Hrishi1999 "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Hrishi1999 "Code") | [
Bhanu Ailani](https://github.com/Bhanu0202)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Bhanu0202 "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Bhanu0202 "Code") | [
Deepak Sharma](https://sourcerer.io/dsdsharma)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=dsdsharma "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=dsdsharma "Code") | [
Ramanpreet Singh](https://github.com/Ramanpreet6262)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Ramanpreet6262 "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Ramanpreet6262 "Code") | [
Anil Khatri](https://imkaka.github.io/)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=imkaka "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=imkaka "Code") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | -| [
Ashwek Swamy](https://github.com/ashwek)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=ashwek "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=ashwek "Code") | [
Dhruv Gupta](https://github.com/dhruv-gupta14)
[πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=dhruv-gupta14 "Code") | +| [
Ashwek Swamy](https://github.com/ashwek)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=ashwek "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=ashwek "Code") | [
Dhruv Gupta](https://github.com/dhruv-gupta14)
[πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=dhruv-gupta14 "Code") | [
Aadit Kamat](https://sg.linkedin.com/in/aadit-kamat-12a5a8140)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=aaditkamat "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=aaditkamat "Code") | This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! diff --git a/Day1/Ruby/fizzbuzz.rb b/Day1/Ruby/fizzbuzz.rb new file mode 100644 index 00000000..e69de29b From 85abd67fe9c0de7fdf924c73447e8050d526cf47 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Fri, 21 Dec 2018 22:38:25 +0800 Subject: [PATCH 02/72] Add Ruby code for Day 1: FizzBuzz problem --- Day1/README.md | 30 ++++++++++++++++++++++++++++++ Day1/Ruby/fizzbuzz.rb | 24 ++++++++++++++++++++++++ Day2/{ => C++}/c++ | 0 3 files changed, 54 insertions(+) rename Day2/{ => C++}/c++ (100%) diff --git a/Day1/README.md b/Day1/README.md index 1e7c6e29..6e8d14bb 100644 --- a/Day1/README.md +++ b/Day1/README.md @@ -309,3 +309,33 @@ func main() { } } ``` +## Ruby Implementation + +### [Fizzbuzz.rb](./Ruby/fizzbuzz.rb) + +```ruby +=begin + @author: aaditkamat + @date: 21/12/2018 +=end + +def get_result(num) + result = '' + if num % 3 != 0 and num % 5 != 0 + result += num.to_s + end + if num % 3 == 0 + result += 'Fizz' + end + if num % 5 == 0 + result += 'Buzz' + end + result +end + +def solution(n) + n.times do |num| + p get_result(num + 1) + end +end +``` \ No newline at end of file diff --git a/Day1/Ruby/fizzbuzz.rb b/Day1/Ruby/fizzbuzz.rb index e69de29b..1d4789db 100644 --- a/Day1/Ruby/fizzbuzz.rb +++ b/Day1/Ruby/fizzbuzz.rb @@ -0,0 +1,24 @@ +=begin + @author: aaditkamat + @date: 21/12/2018 +=end + +def get_result(num) + result = '' + if num % 3 != 0 and num % 5 != 0 + result += num.to_s + end + if num % 3 == 0 + result += 'Fizz' + end + if num % 5 == 0 + result += 'Buzz' + end + result +end + +def solution(n) + n.times do |num| + p get_result(num + 1) + end +end diff --git a/Day2/c++ b/Day2/C++/c++ similarity index 100% rename from Day2/c++ rename to Day2/C++/c++ From 76cf6c51034d4c9c0836e30213cc8bb59c944229 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sat, 22 Dec 2018 10:39:23 +0800 Subject: [PATCH 03/72] Add Ruby code for Day 2: String reverse problem --- Day2/Ruby/reverse.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Day2/Ruby/reverse.rb diff --git a/Day2/Ruby/reverse.rb b/Day2/Ruby/reverse.rb new file mode 100644 index 00000000..3074d26e --- /dev/null +++ b/Day2/Ruby/reverse.rb @@ -0,0 +1,18 @@ +=begin + @author: aaditkamat + @date: 22/12/2018 +=end + +def short_solution(str) + str.reverse! +end + +def long_solution(str) + result = '' + i = 0 + until i >= str.length do + result.insert(0, str[i]) + i += 1 + end + result +end From 39210a0f41a9cbfb00417e8d88f80aa1779fa2cf Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sat, 22 Dec 2018 10:46:19 +0800 Subject: [PATCH 04/72] Update README.md for Day 2 --- Day2/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Day2/README.md b/Day2/README.md index 6ad29b57..27c5ebb3 100644 --- a/Day2/README.md +++ b/Day2/README.md @@ -547,4 +547,28 @@ if d==e: else: print("Not a pallindrome") ``` +##Ruby implementation + +### [reverse.rb](./Ruby/reverse.rb) + +```ruby +=begin + @author: aaditkamat + @date: 22/12/2018 +=end + +def short_solution(str) + str.reverse! +end + +def long_solution(str) + result = '' + i = 0 + until i >= str.length do + result.insert(0, str[i]) + i += 1 + end + result +end +``` From 64a0ca29681eeedf84d9669ca1d15f38ebc66770 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sat, 22 Dec 2018 10:59:32 +0800 Subject: [PATCH 05/72] Modify Ruby code and README --- Day2/README.md | 8 ++++---- Day2/Ruby/reverse.rb | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Day2/README.md b/Day2/README.md index 27c5ebb3..1a21a01e 100644 --- a/Day2/README.md +++ b/Day2/README.md @@ -558,17 +558,17 @@ else: =end def short_solution(str) - str.reverse! + str === str.reverse end def long_solution(str) - result = '' + reverse = '' i = 0 until i >= str.length do - result.insert(0, str[i]) + reverse.insert(0, str[i]) i += 1 end - result + reverse === str end ``` diff --git a/Day2/Ruby/reverse.rb b/Day2/Ruby/reverse.rb index 3074d26e..ada2fd49 100644 --- a/Day2/Ruby/reverse.rb +++ b/Day2/Ruby/reverse.rb @@ -4,15 +4,15 @@ =end def short_solution(str) - str.reverse! + str === str.reverse end def long_solution(str) - result = '' + reverse = '' i = 0 until i >= str.length do - result.insert(0, str[i]) + reverse.insert(0, str[i]) i += 1 end - result + reverse === str end From ab743a6d351edd101eb6e1017cd6980447462085 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sat, 22 Dec 2018 11:14:37 +0800 Subject: [PATCH 06/72] Add condition for nil and wrong type edge cases --- Day2/README.md | 18 +++++++++++++++++- Day2/Ruby/reverse.rb | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Day2/README.md b/Day2/README.md index 1a21a01e..39d86801 100644 --- a/Day2/README.md +++ b/Day2/README.md @@ -558,10 +558,16 @@ else: =end def short_solution(str) + if str.class != String or str === nil + return false + end str === str.reverse end -def long_solution(str) +def long_solution_iterative(str) + if str.class != String or str === nil + return false + end reverse = '' i = 0 until i >= str.length do @@ -570,5 +576,15 @@ def long_solution(str) end reverse === str end + +def long_solution_recursive(str) + if str.class != String or str === nil + return false + end + if str === '' + return true + end + return (str[0] === str[-1] and long_solution(str[1..-2])) +end ``` diff --git a/Day2/Ruby/reverse.rb b/Day2/Ruby/reverse.rb index ada2fd49..525d97f9 100644 --- a/Day2/Ruby/reverse.rb +++ b/Day2/Ruby/reverse.rb @@ -4,10 +4,16 @@ =end def short_solution(str) + if str.class != String or str === nil + return false + end str === str.reverse end -def long_solution(str) +def long_solution_iterative(str) + if str.class != String or str === nil + return false + end reverse = '' i = 0 until i >= str.length do @@ -16,3 +22,14 @@ def long_solution(str) end reverse === str end + +def long_solution_recursive(str) + if str.class != String or str === nil + return false + end + if str === '' + return true + end + return (str[0] === str[-1] and long_solution(str[1..-2])) +end + From bdeefe5d062ba709275bea4c3a0f641eb02c6596 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sat, 22 Dec 2018 22:23:39 +0800 Subject: [PATCH 07/72] Add a seperate Ruby source code file for palindrome --- Day2/Ruby/palindrome.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Day2/Ruby/palindrome.rb diff --git a/Day2/Ruby/palindrome.rb b/Day2/Ruby/palindrome.rb new file mode 100644 index 00000000..4335b16a --- /dev/null +++ b/Day2/Ruby/palindrome.rb @@ -0,0 +1,9 @@ +=begin + @author: aaditkamat + @date: 22/12/2018 +=end +require('./reverse') + +def palindrome(str) + str != nil and str === short_solution(str) +end \ No newline at end of file From fc41b6a17d6265a5caa75f90ca9f19bbbd027148 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sat, 22 Dec 2018 22:24:57 +0800 Subject: [PATCH 08/72] Modify code for reverse.rb --- Day2/Ruby/reverse.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Day2/Ruby/reverse.rb b/Day2/Ruby/reverse.rb index 525d97f9..5d18f2b5 100644 --- a/Day2/Ruby/reverse.rb +++ b/Day2/Ruby/reverse.rb @@ -5,14 +5,14 @@ def short_solution(str) if str.class != String or str === nil - return false + return nil end - str === str.reverse + str.reverse end def long_solution_iterative(str) if str.class != String or str === nil - return false + return nil end reverse = '' i = 0 @@ -20,16 +20,17 @@ def long_solution_iterative(str) reverse.insert(0, str[i]) i += 1 end - reverse === str + reverse end def long_solution_recursive(str) if str.class != String or str === nil - return false + return nil end if str === '' - return true + return '' end - return (str[0] === str[-1] and long_solution(str[1..-2])) + return long_solution_recursive(str[1..-1]) + str[0] end + From 784ee39667f44bc446ccc10e1f0ef029860bc552 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sat, 22 Dec 2018 22:25:37 +0800 Subject: [PATCH 09/72] Add seperate palindrome and reverse code sections in README --- Day2/README.md | 77 +++++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/Day2/README.md b/Day2/README.md index 8da7e265..dbb59355 100644 --- a/Day2/README.md +++ b/Day2/README.md @@ -332,6 +332,50 @@ void main(){ } ``` +## Ruby Implementation + +### [reverse.rb](./Ruby/reverse.rb) + +```ruby +=begin + @author: aaditkamat + @date: 22/12/2018 +=end + +def short_solution(str) + if str.class != String or str === nil + return false + end + str.reverse +end + +def long_solution_iterative(str) + if str.class != String or str === nil + return false + end + reverse = '' + i = 0 + until i >= str.length do + reverse.insert(0, str[i]) + i += 1 + end + reverse +end + +def long_solution_recursive(str) + if str.class != String or str === nil + return false + end + if str === '' + return '' + end + temp = str[0] + str[0] = str[-1] + str[-1] = temp + return long_solution(str[1..-2]) +end +``` +
## Part B -- Palindrome Check @@ -613,7 +657,7 @@ if d==e: else: print("Not a pallindrome") ``` -##Ruby implementation +## Ruby implementation ### [reverse.rb](./Ruby/reverse.rb) @@ -622,35 +666,10 @@ else: @author: aaditkamat @date: 22/12/2018 =end +require('./reverse') -def short_solution(str) - if str.class != String or str === nil - return false - end - str === str.reverse -end - -def long_solution_iterative(str) - if str.class != String or str === nil - return false - end - reverse = '' - i = 0 - until i >= str.length do - reverse.insert(0, str[i]) - i += 1 - end - reverse === str -end - -def long_solution_recursive(str) - if str.class != String or str === nil - return false - end - if str === '' - return true - end - return (str[0] === str[-1] and long_solution(str[1..-2])) +def palindrome(str) + str != nil and str === short_solution(str) end ``` From b24fa8c49978537f5768413dfc4d62a6967b24fd Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sun, 23 Dec 2018 09:56:55 +0800 Subject: [PATCH 10/72] Update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 018c45dc..73b1bb4a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ youtube/ Instagram/ a.out .vscode/ +test/ *.o \ No newline at end of file From b40d3614dbe9a5dbe2001698ea453964600f01b3 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sun, 23 Dec 2018 10:02:40 +0800 Subject: [PATCH 11/72] Refactor palindrome.rb and rename heading in README --- Day2/README.md | 4 ++-- Day2/Ruby/palindrome.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Day2/README.md b/Day2/README.md index dbb59355..ee08aca9 100644 --- a/Day2/README.md +++ b/Day2/README.md @@ -659,14 +659,14 @@ else: ``` ## Ruby implementation -### [reverse.rb](./Ruby/reverse.rb) +### [palindrome.rb](./Ruby/palindrome.rb) ```ruby =begin @author: aaditkamat @date: 22/12/2018 =end -require('./reverse') +require './reverse' def palindrome(str) str != nil and str === short_solution(str) diff --git a/Day2/Ruby/palindrome.rb b/Day2/Ruby/palindrome.rb index 4335b16a..20430262 100644 --- a/Day2/Ruby/palindrome.rb +++ b/Day2/Ruby/palindrome.rb @@ -2,8 +2,8 @@ @author: aaditkamat @date: 22/12/2018 =end -require('./reverse') +require './reverse' -def palindrome(str) +def solution(str) str != nil and str === short_solution(str) -end \ No newline at end of file +end From 5f1a01def14624a2db3c6acceb517ccf257316f4 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 24 Dec 2018 14:15:01 +0800 Subject: [PATCH 12/72] Add solution for Day 3: Hamming Problem --- Day3/C++/hamming | Bin 0 -> 22112 bytes Day3/C++/hamming.cpp | 23 +++++++++++++++++++++++ Day3/README.md | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100755 Day3/C++/hamming create mode 100644 Day3/C++/hamming.cpp diff --git a/Day3/C++/hamming b/Day3/C++/hamming new file mode 100755 index 0000000000000000000000000000000000000000..9ee2cf721da130e2115b14f1b2d0a6354fd9e581 GIT binary patch literal 22112 zcmeHP3v?XSd7iapBc3ahw77Y|LU0g|7|AbW2t))A83oA^ww2>j$XHshEfKwtR$$sF zky&rp@pwRO*~STQqU3ObA!*`}$WdF?$Uu=w+#)z_;-n-|fh4mjZ4{al#3QKR_uo7F zl5B|c=;=9{nHxSrYVi{)M(4z+rxF#|_9k=lreYB(UsX&qG!EMgm}4x~Y}<~KnR9>APMlAzq{}r&widJK7uX zY3tms>81K!U#|51spbWV`C_H-KCOg!sg6SZjFTFC zK-*q-<)$kOIEV*{69y=sAYL@iI6(}{OSWxyw%fh8cC>W0+uiY=qv{LPQ6I|(55H$5 zKmcufdsji&&C*wa^0Y4~L%dYqIYs)~n~!B*mDZ;kjpml+9BEW3H@TSN;3=dE*uQNBXc4^9#?wxM5hqDrzRfA04P5|U z1>WGPM02U9DjZN0WWDqYd2DmpiGxrX%G&~aq8DDcym~o|YHJZYYYOBSjlTe|70YkD zwKpE?SpGp>1k&o8(w?R8X9Vzkyu|6>`Dfk(^B$P@z`O_MJ#aJ+G+EA%t(`BHV{#fx z9)4qHhJs_sahu09B-+@6sRk=9B&APvc(Ff zsK@mLINr3v1UR0d6cB_DEIIUQ$cjFxwZH-@LrF1i?V)huU6{p7+uyL)a?C-?H3^3R zhXD6kZqq)?Y1$9CA8-V4Bz$0n^OBW#4sCvwZQcv*;aynUvav)^(6`(M-*OtJ0H*+_ z0rA#Vz^FlpWG^ie9|e09V&j10fD?cdR^m_y`6I|50h|P!)WuN|wSY1hKKSXE03KQTYYXln>;0)I)On@_NDU6`#13T$GqCYtR5Z<*~wSfpK+aZNK z0ZzjJY#V^pDL}l^@Es7$ZZ0a+s0s;iyv+&|;CNdUCO}-_wJM4L$Lm&@0LPP?FI1%r6X19?3KQUX;$b%z!mHgzH@a z=&lUddS?tO*@ygBvCs{|_$ba&eNBA?H@$;ZJ5Bf&c%3GE2fR+29ve%xBc;KK408*^ z3G7CpK1r_Yk>)&Ujg?JMM&bAfjxe(0;1;@I8s_0+qx;}>(3MN`*jY?A!P6njgaJY;peP`)cE3L$6m&u_BbK(1bql_zLw!?_cDxs<^{zQ+ zH_ky@Hk)QCXZVYQYKx4SIzwBR<*pR65<%Yw)s-Ovg9^uw8y*8oOnf9)0P?v44-`|~ z3~l#KP{Su%91+r0B)kJ|+qS%Dd`i3+L64iZoIdSLLKAaPKe`JF_D%Y$pazac7wVg| zoJaj_#1oH4Zg^}PpJIJGUmW6W>0~NVA=VBN0(V$2FS8U_^)tQ^m}_}SlHhFGhtY&! zcX`VD;jaOcO!tMtNuKIuzlL)bNQgowX(@RD7zdS-9YR!NyWZ(J!6;OIq_0?tk4+5O6YSsq&IRMj}ICgzwxd6$fA1z?ce20l~6Rtqz?e zdkc61oa9o42|z?COhB-HQOg)a4|!wBY1GFDDwwqrJ{DX_T9JJ_Y>-i)0+{2SLfT>U z#JrVDXyu4fF!L#0j!Mxauo$Z)e(*BBH1`QV^S0celvFgXCHwKNK7et;{|xxhI0|t+ zHcB3(5o%D1{O5C8Tt$bY!5&M#fK;16YplzpO=0g4yXGvj9IRmr*RgJGB`Vs_g02FN zIe}3Y@3l*<%dU-ZfdOtfi34QnBVxJy+!n@+nWvrPeV~Zpu4Ena*(^`|}qGfbV#JxKj8LC}?~^Mrppkldi~321ety@~M!j%2d` zj?Yx`=>m^^9c9HX?bQswuK$mqVhU7WE3BgSDY#&eb?*v_`1gsJ`lve}=P-A}W6Df_ z2EmX@{s{8u6#NFe72-7bW9AD+Ex6|MaG@VQ*#A7-m!jscpipAk2=CG>DH1K$st6x! zdftk@mq1r_~0;Y^gB^=5LXf)SkI!plAR+wrqqmpnb+kj;jsgH@HquI<|x{RHy0k=;O4@E zhzgHJ9UkqV=79n2)XOcGaL_2~<60jkma6`pxx{;m;& zhSeylFjry6*#2R{Z&bDmP_5N%MC-OF9zpm(xifAhCNT$3v${%EU4*P)b4&yaK8%W` z+XQYE>M&NzNkFz9#-kD?E?(o*B!x?NeI&YSza!IQ$;LA{T2NDoZ0P$CM`|u{uoIEp z2t%x4L~S_o`|y%gh3o~;=JLW85jnH=|_LU zn$XuKqO!75|5%JLZr?=x*~zuzU!5rc>t2*nv%3?h4Djxdx-) z%TE72mh;_pt_jdLxXyV3{K1)LQs$jm-Xk)c&2gvUam(wU@Lz#iQHi?7{SII{h?@p{ zllD{SU&W^W{kzRO!w0AQFDMk>NxHKCnLOStpJ5taVdLSI@LB6{2jxFZ5vS~2|8J2Q z)cs*-c7JfvmEv)A9D-#nF#mFtQ8r>xD-|SbMM?k{wJ&1`-koHc8=-GP_p?0x<<#C^ z?dlN;mMX0}l)@a@ZiCmVc+wCZ>n>Ja8?fGjX zPp`~}#T9VQ^G*1R_~py9JzJ;T)`=mAE=* za?`42MgnSn2^J~B6_)!c5Z*vMAQ16lWe&w?&bWZ|r~p|x`+t2BYx71xa=*^6=#2Rt zpyfRKD`gG^5J?Q=hzevx$`ugQpNe6f3*k#KI{NRVW_2`~k7BiOo;#2;Uy(Wx%Sn|R z9s)rkI?Wt{I{A6P><4IOQCy#$GsHE=T?D&2RdBpLls{J@kGr=4p#Jroml){(d7?Id zx9ANY9LGT{8|v(q&WzO{5I036^<*mjDPWiYI6Ul55?Loj^ueq;!ga;TI7_0F{Vzz( z$!_F>ke(JLYN@Y}Q-c99QG>bli^|`#qH1u=+{1MUf@{!i+QY%YX}Sm4kW8QV1oPTC zL8GL?>_+V?S=(d%4coJJKpTWD+D9N^0Z4Zt8c0XDR6y8CvIy3FO9A}*ajz^pY~30~ z{Yz4*AX~$PIk<_X{fEFpUbUuRN!W@S{F{PuJ~bfbMGQ+q8DiB$OcTUvzax+?s^9_K zrspPND*fbZGc$ASrxPz$$o?D$9sb3vE%A5-j~Fpt>G5e23(<9{@p>;kfdHxZ?yV3n z$Yu&%Sr&CpdWyk=Ju5}j3Ol!6H6UN_+#6xBG1-s9c(^ky6nA*3=#vu!=u-u!><C#>@^ZAmVRvkn98Ar(2wi4O|v9HGuchQJ8l@m(b)#%d{aD~e!G zNRI?zpPo|IsreQ|YTXtLVyc5E9(O_5b+K@G*Pm0br*=|aaG}AlgmnxyK5&Il;7VeS zK9WyI6i=p`F@zV}5vbTNreQ@sCJt82B46KxC8mnHL`M7&zT+L3a0-uN_fm-~+;zE4 z3#R%0ed|)@{g}S7=0Scp^1N?w`bR;31a#!NBkafc*?$D^NS*UaJys0$a_x|5h2DAx zyC`o9a(<_Lb&ph(^HJUhoWh7airOiHUC$%_Q!v=_JZ03d3Y+6QAIFliQG6#LeBcBu zToOOUYI6pI`|xJ5!)s){!D^ETMyCPTHUQgFfGNO1K)kZrv>+1PNGNexz2lQ?R$pz4 z`dk8nNm|X->M*8T@dcF3(jJu~NCgC2a-0?kHLVOz`>G>N3h<7oFchLpsay=kYbc)FF+y#=^!W=>RH;$>;>>M7<97h!B^tQzp=GipL9^(%de zpAKN0$k#u!tO_AAvuuT=t0Y|`X^o_pO8S0DuaLA>(ho|yPSUF-wIuzpr1g@nm$XsR z4U%q>^rMn)mh?JFub1>=l5UZ-MbfR3Zj-cC(i-;W0%x{f#oV*Z@yH_k40C_S+&FWmvVR<8?gr+*$=n0XJ;>Y?b3@E6XRrJl<~o_% z!`wHSyN$V*ncKnKO3XjmPUae!~){4n`T-70msb zG){`Gr*Mip6FGdJyZ43`-U*8g<4H+%<^mv3p@8uUtl)~|c_K@YTo3p%kxwGQ_Dyl_ z)QFUMP$U09vBjwD9KhQFLzPID0g9@=|Drjc1jqP3kf#9Uc>o{!5NZK7u#&H6(wASDv8!m(iYkLR3pbcf$Hb{{|il zrHT9;2n#i#r3*iXgq$Sc33nm+VIcOH_CXX)S@b=Ph&F!riy$V?65Z@e0F8GeIbSo3 zt630;D7XeluBEWNKZKW(`SzYo4Sbkk!ATN+j)d(P#wAneH6Y(s?F0s@VVIrqSWhJ0 z8jEae>*7V;Bzu!k=j4$wrOTh~M=tM5Y|)9_2=;q5{m*0pDgUh`h}R4dI4v z^b8|32pz}!48SI~Z4$N;9==e)?`XJ1()Nnw<{Tg<|e$k`L{Y=AOYWRkR<+{Gp zHGG$b@71tM!%H=!f2enz#@B1OS;Gm86;kgz6{LTOw`hL5hBs@tTf@6Fgy{S;?}2#_ z%zI$o1M?o3_rSad<~=a)fq4(idtlxJ^B(wr=Yb{XRBegC>Ls(sDP1Ljonxgp6 z(j{u#?>}$-1|Gwx2{@waHv?9?hnCNpVNirn%-DvyysS$ zv~i`~LX-B*sV3-FbE{d2{;C|b|2Rso-qsE;t453*NBQDLunnWfuO;y2_;ILc?={<4 z{gI-wx^o1ICySW5&ae^_QEx1cSlimt7PoP@ZsRPzw+*w0-4lzq_H^B%Jmk+^es#1x z*4$&>$of+Yq%UoZuf^zz+mMK(dR;1hjCvhKchanXrVE22CfVCF36w$k$xWzwpb5z!ll@F4m`pa|zUhp~#X~1Hj)tce4lbw|I59GDYNR3(;f<0! z&*4t(%+P?`OY#1s64#N$C-wbHt;TszMgBK|pN1pr=baaEcAqdZ!QXq@EI8`d_e(L22lr9zo?+Un`G2qR;C|}I8V~NN7DG4XgZrxG z8V~NR_`?$NgZrjojR*Hj(;A=D_e>v$!ITf~n|>g;awkNMUkI-5{fP5&9T2H{_>+F% zWXZF<1g|f_KU0FgP=db?-=a`oPYM1|37##%&skJlzPSXyw*-Ht1b?ms|DO{4WZu~p zwD;^1{JIkS<`Vq&68v)|_?JuYLkiFN`=3hir%Q1D)Pk4axKfqB33L+R$#_q}dn(@3 z@Zt(r{(c&NUX3u2`(Rw-8oYkxFPI{D&&GQWUPi)u@t%j5_r&5$#(8Iiyy^R$8M@@;PMr!bHO~Wj|B6C;se1vy>QEyr{*I5t?l*Z zD45&ifmgh{v+;K>OSW~Il;6*RFvoFau)PcRb+gVc^LDI)uJ*alp61?e1e=6QY|N5& zw7t1=JNB0C?f9FfdSA-fQ*Ym@(S5s{x60NRN3S?iI9gTQ@2u5dvZZgE-3E1X&=%Ck tqdFY=ZLHxxo^s_}Z-Q)m=&Gv<{=#T`ta=t@ + +using namespace std; + +int hammingDistance(string first_word, string second_word) { + int count = 0; + for (int i = 0; i < first_word.size(); i++) { + if (first_word[i] != second_word[i]) { + count++; + } + } + return count; +} + +int main() { + string first_word, second_word; + cout << "Enter the first word: "; + cin >> first_word; + cout << "Enter the second word: "; + cin >> second_word; + cout << "The hamming distance between " << first_word << " and " << second_word << " is: " << hammingDistance(first_word, second_word) << endl; + return 0; +} \ No newline at end of file diff --git a/Day3/README.md b/Day3/README.md index c225fa7d..a3ccc403 100644 --- a/Day3/README.md +++ b/Day3/README.md @@ -95,7 +95,41 @@ public class HammingDistance { } ``` -### Why HAmming Distance? +## C++ Implementation + +### [hamming.cpp](./C++/hamming.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 24/12/2018 + */ + #include + #include + + using namespace std; + + int hammingDistance(string first_word, string second_word) { + int count = 0; + for (int i = 0; i < first_word.size(); i++) { + if (first_word[i] != second_word[i]) { + count++; + } + } + return count; + } + + int main() { + string first_word, second_word; + cout << "Enter the first word: "; + cin >> first_word; + cout << "Enter the second word: "; + cin >> second_word; + cout << "The hamming distance between " << first_word << " and " << second_word << " is: " << hammingDistance(first_word, second_word) << endl; + return 0; + } +``` +## Why Hamming Distance? The #1 reason for not being successful is inconsistency, and it is a common trend that people start something and on give up on the third day itself, and one of the major reasons behind that is that they find it difficult to continue.Β  So, to keep everyone (who is following the DailyCodes challenge) consistent, I have decided to keep today's question very simple and straightforward. \ No newline at end of file From 8d3721675b489a6425d74e707233b866a1bccef4 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 24 Dec 2018 15:11:21 +0800 Subject: [PATCH 13/72] Add condition for strings of unequal lengths --- Day3/C++/hamming.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Day3/C++/hamming.cpp b/Day3/C++/hamming.cpp index 7066c945..67974912 100644 --- a/Day3/C++/hamming.cpp +++ b/Day3/C++/hamming.cpp @@ -4,6 +4,9 @@ using namespace std; int hammingDistance(string first_word, string second_word) { int count = 0; + if (first_word.size() != second_word.size()) { + return -1; + } for (int i = 0; i < first_word.size(); i++) { if (first_word[i] != second_word[i]) { count++; From 3183f2d63c92ecaaa7da793e893192c5798752df Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 24 Dec 2018 15:25:39 +0800 Subject: [PATCH 14/72] Update README --- Day3/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Day3/README.md b/Day3/README.md index a3ccc403..cf1e0267 100644 --- a/Day3/README.md +++ b/Day3/README.md @@ -111,6 +111,9 @@ public class HammingDistance { int hammingDistance(string first_word, string second_word) { int count = 0; + if (first_word.size() != second_word.size()) { + return -1; + } for (int i = 0; i < first_word.size(); i++) { if (first_word[i] != second_word[i]) { count++; From 08c9ca18d2900ec6d589bb863fbcf07df3056248 Mon Sep 17 00:00:00 2001 From: Aadit Date: Tue, 25 Dec 2018 11:19:47 +0800 Subject: [PATCH 15/72] Change project name and owner in.all-contributorsrc --- .all-contributorsrc | 4 ++-- CONTRIBUTORS.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index a8cda2ee..ca0e4558 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1,6 +1,6 @@ { - "projectName": "DailyCodeBase", - "projectOwner": "MadhavBahlMD", + "projectName": "dailycodebase", + "projectOwner": "CodeToExpress", "repoType": "github", "repoHost": "https://github.com", "files": [ diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 42a33c5f..be8d198a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -6,9 +6,9 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds -| [
MADHAV BAHL](http://madhavbahl.tech/)
[πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=MadhavBahlMD "Code") [πŸ“](#blog-MadhavBahlMD "Blogposts") [πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=MadhavBahlMD "Documentation") [🎨](#design-MadhavBahlMD "Design") [βœ…](#tutorial-MadhavBahlMD "Tutorials") | [
Rajdeep Roy Chowdhury](http://www.linkedin.com/in/razdeeproychowdhury)
[πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Razdeep "Code") [πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Razdeep "Documentation") [βœ…](#tutorial-Razdeep "Tutorials") | [
Hrishi](https://hrishi1999.github.io)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Hrishi1999 "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Hrishi1999 "Code") | [
Bhanu Ailani](https://github.com/Bhanu0202)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Bhanu0202 "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Bhanu0202 "Code") | [
Deepak Sharma](https://sourcerer.io/dsdsharma)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=dsdsharma "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=dsdsharma "Code") | [
Ramanpreet Singh](https://github.com/Ramanpreet6262)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Ramanpreet6262 "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Ramanpreet6262 "Code") | [
Anil Khatri](https://imkaka.github.io/)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=imkaka "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=imkaka "Code") | +| [
MADHAV BAHL](http://madhavbahl.tech/)
[πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=MadhavBahlMD "Code") [πŸ“](#blog-MadhavBahlMD "Blogposts") [πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=MadhavBahlMD "Documentation") [🎨](#design-MadhavBahlMD "Design") [βœ…](#tutorial-MadhavBahlMD "Tutorials") | [
Rajdeep Roy Chowdhury](http://www.linkedin.com/in/razdeeproychowdhury)
[πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Razdeep "Code") [πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Razdeep "Documentation") [βœ…](#tutorial-Razdeep "Tutorials") | [
Hrishi](https://hrishi1999.github.io)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Hrishi1999 "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Hrishi1999 "Code") | [
Bhanu Ailani](https://github.com/Bhanu0202)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Bhanu0202 "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Bhanu0202 "Code") | [
Deepak Sharma](https://sourcerer.io/dsdsharma)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=dsdsharma "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=dsdsharma "Code") | [
Ramanpreet Singh](https://github.com/Ramanpreet6262)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Ramanpreet6262 "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Ramanpreet6262 "Code") | [
Anil Khatri](https://imkaka.github.io/)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=imkaka "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=imkaka "Code") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | -| [
Ashwek Swamy](https://github.com/ashwek)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=ashwek "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=ashwek "Code") | [
Dhruv Gupta](https://github.com/dhruv-gupta14)
[πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=dhruv-gupta14 "Code") | [
Aadit Kamat](https://sg.linkedin.com/in/aadit-kamat-12a5a8140)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=aaditkamat "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=aaditkamat "Code") | [
Shashank jain](http://www.myshashank.tech)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Shashankjain12 "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Shashankjain12 "Code") | +| [
Ashwek Swamy](https://github.com/ashwek)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=ashwek "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=ashwek "Code") | [
Dhruv Gupta](https://github.com/dhruv-gupta14)
[πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=dhruv-gupta14 "Code") | [
Aadit Kamat](https://sg.linkedin.com/in/aadit-kamat-12a5a8140)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=aaditkamat "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=aaditkamat "Code") | [
Shashank jain](http://www.myshashank.tech)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Shashankjain12 "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Shashankjain12 "Code") | This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! From af34b2abbde4e18d56d85049d183ffc5567da83e Mon Sep 17 00:00:00 2001 From: Aadit Date: Tue, 25 Dec 2018 12:19:39 +0800 Subject: [PATCH 16/72] Remove merge conflict lines --- Day3/README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Day3/README.md b/Day3/README.md index ea2f5461..a2818375 100644 --- a/Day3/README.md +++ b/Day3/README.md @@ -106,11 +106,6 @@ public class HammingDistance { */ #include #include -<<<<<<< HEAD - - using namespace std; - -======= using namespace std; From 6db4527af58db0b5065f382cd5a65b8772fd5df4 Mon Sep 17 00:00:00 2001 From: Aadit Date: Tue, 25 Dec 2018 20:57:50 +0800 Subject: [PATCH 17/72] Add @shivank86 as a contributor --- .all-contributorsrc | 9 +++++++++ CONTRIBUTORS.md | 5 ++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 9c040af0..d8fd76ee 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -143,6 +143,15 @@ "doc", "code" ] + }, + { + "login": "shivank86", + "name": "shivank86", + "avatar_url": "https://avatars2.githubusercontent.com/u/43927436?v=4", + "profile": "https://github.com/shivank86", + "contributions": [ + "code" + ] } ] } diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 61d75553..b4361265 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,4 +1,4 @@ -[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors) ## Contributors Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): @@ -7,8 +7,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds | [
MADHAV BAHL](http://madhavbahl.tech/)
[πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=MadhavBahlMD "Code") [πŸ“](#blog-MadhavBahlMD "Blogposts") [πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=MadhavBahlMD "Documentation") [🎨](#design-MadhavBahlMD "Design") [βœ…](#tutorial-MadhavBahlMD "Tutorials") | [
Rajdeep Roy Chowdhury](http://www.linkedin.com/in/razdeeproychowdhury)
[πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Razdeep "Code") [πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Razdeep "Documentation") [βœ…](#tutorial-Razdeep "Tutorials") | [
Hrishi](https://hrishi1999.github.io)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Hrishi1999 "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Hrishi1999 "Code") | [
Bhanu Ailani](https://github.com/Bhanu0202)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Bhanu0202 "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Bhanu0202 "Code") | [
Deepak Sharma](https://sourcerer.io/dsdsharma)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=dsdsharma "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=dsdsharma "Code") | [
Ramanpreet Singh](https://github.com/Ramanpreet6262)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Ramanpreet6262 "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Ramanpreet6262 "Code") | [
Anil Khatri](https://imkaka.github.io/)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=imkaka "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=imkaka "Code") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | -| [
Ashwek Swamy](https://github.com/ashwek)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=ashwek "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=ashwek "Code") | [
Dhruv Gupta](https://github.com/dhruv-gupta14)
[πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=dhruv-gupta14 "Code") | [
Aadit Kamat](https://sg.linkedin.com/in/aadit-kamat-12a5a8140)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=aaditkamat "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=aaditkamat "Code") | [
Shashank jain](http://www.myshashank.tech)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Shashankjain12 "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=Shashankjain12 "Code") | [
Moulik Aggarwal](https://github.com/moulikcipherX)
[πŸ’¬](#question-moulikcipherX "Answering Questions") [πŸ“](#blog-moulikcipherX "Blogposts") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=moulikcipherX "Code") [πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=moulikcipherX "Documentation") | [
Prateek Surana](http://prateeksurana.me)
[πŸ“–](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=prateek3255 "Documentation") [πŸ’»](https://github.com/MadhavBahlMD/DailyCodeBase/commits?author=prateek3255 "Code") | ->>>>>>> 64de4beee69e4f49e4b1eb0d6b8935e96fb1251b +| [
Ashwek Swamy](https://github.com/ashwek)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=ashwek "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=ashwek "Code") | [
Dhruv Gupta](https://github.com/dhruv-gupta14)
[πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=dhruv-gupta14 "Code") | [
Aadit Kamat](https://sg.linkedin.com/in/aadit-kamat-12a5a8140)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=aaditkamat "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=aaditkamat "Code") | [
Shashank jain](http://www.myshashank.tech)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=Shashankjain12 "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=Shashankjain12 "Code") | [
Moulik Aggarwal](https://github.com/moulikcipherX)
[πŸ’¬](#question-moulikcipherX "Answering Questions") [πŸ“](#blog-moulikcipherX "Blogposts") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=moulikcipherX "Code") [πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=moulikcipherX "Documentation") | [
Prateek Surana](http://prateeksurana.me)
[πŸ“–](https://github.com/CodeToExpress/dailycodebase/commits?author=prateek3255 "Documentation") [πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=prateek3255 "Code") | [
shivank86](https://github.com/shivank86)
[πŸ’»](https://github.com/CodeToExpress/dailycodebase/commits?author=shivank86 "Code") | This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! From e4ea3aded6956c0da7f4b4f414e420a8420a8cd0 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Wed, 26 Dec 2018 23:49:54 +0800 Subject: [PATCH 18/72] Add C++ files for different patterns --- day5/C++/eighth_pattern.cpp | 32 ++++++++++++++++++++++++++++++++ day5/C++/fifth_pattern.cpp | 26 ++++++++++++++++++++++++++ day5/C++/first_pattern.cpp | 20 ++++++++++++++++++++ day5/C++/fourth_pattern.cpp | 31 +++++++++++++++++++++++++++++++ day5/C++/second_pattern.cpp | 21 +++++++++++++++++++++ day5/C++/seventh_pattern.cpp | 32 ++++++++++++++++++++++++++++++++ day5/C++/sixth_pattern.cpp | 32 ++++++++++++++++++++++++++++++++ day5/C++/third_pattern.cpp | 26 ++++++++++++++++++++++++++ day5/README.md | 2 +- 9 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 day5/C++/eighth_pattern.cpp create mode 100644 day5/C++/fifth_pattern.cpp create mode 100644 day5/C++/first_pattern.cpp create mode 100644 day5/C++/fourth_pattern.cpp create mode 100644 day5/C++/second_pattern.cpp create mode 100644 day5/C++/seventh_pattern.cpp create mode 100644 day5/C++/sixth_pattern.cpp create mode 100644 day5/C++/third_pattern.cpp diff --git a/day5/C++/eighth_pattern.cpp b/day5/C++/eighth_pattern.cpp new file mode 100644 index 00000000..07fba24b --- /dev/null +++ b/day5/C++/eighth_pattern.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +void eighth_pattern(int num) { + string result = ""; + int bound = 2 * num, ctr = 1; + for (int i = 1; i <= bound; i++) { + for (int j = 1; j <= bound; j++) { + if (j <= ctr || j > bound - ctr) { + cout << "* "; + } else { + cout << " "; + } + } + if (i < num) { + ctr++; + } + if (i > num) { + ctr--; + } + cout << "\n"; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + eighth_pattern(num); + return 0; +} \ No newline at end of file diff --git a/day5/C++/fifth_pattern.cpp b/day5/C++/fifth_pattern.cpp new file mode 100644 index 00000000..571295bd --- /dev/null +++ b/day5/C++/fifth_pattern.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +void fifth_pattern(int num) { + string result = ""; + int bound = 2 * num - 1; + for (int i = num; i >= 1; i--) { + for (int j = 1; j <= bound; j++) { + if (j <= num - i || j >= num + i) { + cout << " "; + } else { + cout << "* "; + } + } + cout << "\n"; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + fifth_pattern(num); + return 0; +} \ No newline at end of file diff --git a/day5/C++/first_pattern.cpp b/day5/C++/first_pattern.cpp new file mode 100644 index 00000000..f6235ee1 --- /dev/null +++ b/day5/C++/first_pattern.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +void first_pattern(int num) { + for (int i = 1; i <= num; i++) { + for (int j = 1; j <= i; j++) { + cout << j << " "; + } + cout << endl; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + first_pattern(num); + return 0; +} \ No newline at end of file diff --git a/day5/C++/fourth_pattern.cpp b/day5/C++/fourth_pattern.cpp new file mode 100644 index 00000000..6fba1988 --- /dev/null +++ b/day5/C++/fourth_pattern.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +void fourth_pattern(int num) { + int bound = 2 * num - 1, ctr; + for (int i = 1; i <= num; i++) { + ctr = i; + for (int j = 1; j <= bound; j++) { + if (j <= num - i || j >= num + i) { + cout << " "; + } else { + cout << ctr << " "; + if (j < num) { + ctr++; + } else { + ctr--; + } + } + } + cout << endl; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + fourth_pattern(num); + return 0; +} \ No newline at end of file diff --git a/day5/C++/second_pattern.cpp b/day5/C++/second_pattern.cpp new file mode 100644 index 00000000..9834d730 --- /dev/null +++ b/day5/C++/second_pattern.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +void second_pattern(int num) { + int ctr = 1; + for (int i = 1; i <= num; i++) { + for (int j = 1; j <= i; j++) { + cout << ctr++ << " "; + } + cout << endl; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + second_pattern(num); + return 0; +} \ No newline at end of file diff --git a/day5/C++/seventh_pattern.cpp b/day5/C++/seventh_pattern.cpp new file mode 100644 index 00000000..d4b347a4 --- /dev/null +++ b/day5/C++/seventh_pattern.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +void seventh_pattern(int num) { + string result = ""; + int ctr = num, bound = 2 * num + 1; + for (int i = 1; i <= bound - 1; i++) { + for (int j = 1; j <= bound; j++) { + if (j <= ctr || j > bound - ctr) { + cout << "* "; + } else { + cout << " "; + } + } + if (i < num) { + ctr--; + } + if (i > num) { + ctr++; + } + cout << "\n"; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + seventh_pattern(num); + return 0; +} \ No newline at end of file diff --git a/day5/C++/sixth_pattern.cpp b/day5/C++/sixth_pattern.cpp new file mode 100644 index 00000000..f0ae179c --- /dev/null +++ b/day5/C++/sixth_pattern.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +string sixth_pattern(int num) { + string result = ""; + int bound = 2 * num - 1, ctr = 0; + for (int i = 1; i <= bound; i++) { + for (int j = 1; j <= bound; j++) { + if (j < num - ctr || j > num + ctr) { + cout << " "; + } else { + cout << "* "; + } + } + if (i < num) { + ctr++; + } else { + ctr--; + } + cout << endl; + } + return result; +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + sixth_pattern(num); + return 0; +} \ No newline at end of file diff --git a/day5/C++/third_pattern.cpp b/day5/C++/third_pattern.cpp new file mode 100644 index 00000000..969bc118 --- /dev/null +++ b/day5/C++/third_pattern.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +void third_pattern(int num) { + int ctr = 1, bound = 2 * num - 1; + for (int i = 1; i <= bound; i++) { + for (int j = 1; j <= ctr; j++) { + cout << j << " "; + } + if (i < num) { + ctr++; + } else { + ctr--; + } + cout << endl; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + third_pattern(num); + return 0; +} \ No newline at end of file diff --git a/day5/README.md b/day5/README.md index 1667a055..969bcc5c 100644 --- a/day5/README.md +++ b/day5/README.md @@ -1,4 +1,4 @@ -# Day 2 -- Patterns +# Day 5 -- Patterns Pattern programs like printing a pyramid, inverted pyramid etc. are some very famous problems and a good way to test your logic and knowledge of nested loops. Hence, on the day 5 of Daily Codes, let’s do some pattern based programs πŸ˜€ From b74a9e5bf6383cac0a5512eb6e0276ebb09da1d5 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Thu, 27 Dec 2018 00:13:10 +0800 Subject: [PATCH 19/72] Add author and date comments at the top of C++ files --- day5/C++/eighth_pattern.cpp | 5 +++++ day5/C++/fifth_pattern.cpp | 5 +++++ day5/C++/first_pattern.cpp | 5 +++++ day5/C++/fourth_pattern.cpp | 5 +++++ day5/C++/second_pattern.cpp | 5 +++++ day5/C++/seventh_pattern.cpp | 5 +++++ day5/C++/sixth_pattern.cpp | 5 +++++ day5/C++/third_pattern.cpp | 5 +++++ 8 files changed, 40 insertions(+) diff --git a/day5/C++/eighth_pattern.cpp b/day5/C++/eighth_pattern.cpp index 07fba24b..8d806d1b 100644 --- a/day5/C++/eighth_pattern.cpp +++ b/day5/C++/eighth_pattern.cpp @@ -1,3 +1,8 @@ +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + #include using namespace std; diff --git a/day5/C++/fifth_pattern.cpp b/day5/C++/fifth_pattern.cpp index 571295bd..c87dcad9 100644 --- a/day5/C++/fifth_pattern.cpp +++ b/day5/C++/fifth_pattern.cpp @@ -1,3 +1,8 @@ +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + #include using namespace std; diff --git a/day5/C++/first_pattern.cpp b/day5/C++/first_pattern.cpp index f6235ee1..9a357d37 100644 --- a/day5/C++/first_pattern.cpp +++ b/day5/C++/first_pattern.cpp @@ -1,3 +1,8 @@ +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + #include using namespace std; diff --git a/day5/C++/fourth_pattern.cpp b/day5/C++/fourth_pattern.cpp index 6fba1988..c1b04bba 100644 --- a/day5/C++/fourth_pattern.cpp +++ b/day5/C++/fourth_pattern.cpp @@ -1,3 +1,8 @@ +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + #include using namespace std; diff --git a/day5/C++/second_pattern.cpp b/day5/C++/second_pattern.cpp index 9834d730..5551f2e3 100644 --- a/day5/C++/second_pattern.cpp +++ b/day5/C++/second_pattern.cpp @@ -1,3 +1,8 @@ +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + #include using namespace std; diff --git a/day5/C++/seventh_pattern.cpp b/day5/C++/seventh_pattern.cpp index d4b347a4..33481038 100644 --- a/day5/C++/seventh_pattern.cpp +++ b/day5/C++/seventh_pattern.cpp @@ -1,3 +1,8 @@ +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + #include using namespace std; diff --git a/day5/C++/sixth_pattern.cpp b/day5/C++/sixth_pattern.cpp index f0ae179c..cbd45230 100644 --- a/day5/C++/sixth_pattern.cpp +++ b/day5/C++/sixth_pattern.cpp @@ -1,3 +1,8 @@ +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + #include using namespace std; diff --git a/day5/C++/third_pattern.cpp b/day5/C++/third_pattern.cpp index 969bc118..34557976 100644 --- a/day5/C++/third_pattern.cpp +++ b/day5/C++/third_pattern.cpp @@ -1,3 +1,8 @@ +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + #include using namespace std; From 0d167717e6ccad0e47151b028fbfa9e2b11d18f4 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Thu, 27 Dec 2018 00:13:25 +0800 Subject: [PATCH 20/72] Update README.md --- day5/README.md | 325 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 318 insertions(+), 7 deletions(-) diff --git a/day5/README.md b/day5/README.md index 65969b90..3638bb6f 100644 --- a/day5/README.md +++ b/day5/README.md @@ -205,9 +205,41 @@ def pattern1(num): pattern1(5) ``` +## C++ Implementation + +### [first_pattern.cpp](./C++/first_pattern.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + +#include + +using namespace std; + +void first_pattern(int num) { + for (int i = 1; i <= num; i++) { + for (int j = 1; j <= i; j++) { + cout << j << " "; + } + cout << endl; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + first_pattern(num); + return 0; +} +``` + ## Pattern 2 (Floyd's Triangle) -```js +``` input = 4 1 2 3 @@ -297,10 +329,42 @@ def pattern2(num): pattern2(4) ``` +## C++ Implementation + +### [second_pattern.cpp](./C++/second_pattern.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + +#include + +using namespace std; + +void second_pattern(int num) { + int ctr = 1; + for (int i = 1; i <= num; i++) { + for (int j = 1; j <= i; j++) { + cout << ctr++ << " "; + } + cout << endl; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + second_pattern(num); + return 0; +} +``` ## Pattern 3 -```js +``` input = 5 1 1 2 @@ -401,9 +465,47 @@ def pattern3(num): pattern3(4) ``` +## C++ Implementation + +### [third_pattern.cpp](./C++/third_pattern.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + +#include + +using namespace std; + +void third_pattern(int num) { + int ctr = 1, bound = 2 * num - 1; + for (int i = 1; i <= bound; i++) { + for (int j = 1; j <= ctr; j++) { + cout << j << " "; + } + if (i < num) { + ctr++; + } else { + ctr--; + } + cout << endl; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + third_pattern(num); + return 0; +} +``` + ## Pattern 4 -```js +``` input = 5 1 2 3 2 @@ -489,9 +591,52 @@ public class Pattern4 { } ``` +## C++ Implementation + +### [fourth_pattern.cpp](./C++/fourth_pattern.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + +#include + +using namespace std; + +void fourth_pattern(int num) { + int bound = 2 * num - 1, ctr; + for (int i = 1; i <= num; i++) { + ctr = i; + for (int j = 1; j <= bound; j++) { + if (j <= num - i || j >= num + i) { + cout << " "; + } else { + cout << ctr << " "; + if (j < num) { + ctr++; + } else { + ctr--; + } + } + } + cout << endl; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + fourth_pattern(num); + return 0; +} +``` + ## Pattern 5 -```js +``` input = 5 * * * * * * * * * * * * * * * * @@ -569,9 +714,47 @@ public class Pattern5 { } ``` +## C++ Implementation + +### [fifth_pattern.cpp](./C++/fifth_pattern.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + +#include + +using namespace std; + +void fifth_pattern(int num) { + string result = ""; + int bound = 2 * num - 1; + for (int i = num; i >= 1; i--) { + for (int j = 1; j <= bound; j++) { + if (j <= num - i || j >= num + i) { + cout << " "; + } else { + cout << "* "; + } + } + cout << "\n"; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + fifth_pattern(num); + return 0; +} +``` + ## Pattern 6 -```js +``` input = 5 * * * * @@ -686,9 +869,52 @@ function pattern6 (n) { pattern6 (5); ``` +## C++ Implementation + +### [sixth_pattern.cpp](./C++/sixth_pattern.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ +#include + +using namespace std; + +string sixth_pattern(int num) { + string result = ""; + int bound = 2 * num - 1, ctr = 0; + for (int i = 1; i <= bound; i++) { + for (int j = 1; j <= bound; j++) { + if (j < num - ctr || j > num + ctr) { + cout << " "; + } else { + cout << "* "; + } + } + if (i < num) { + ctr++; + } else { + ctr--; + } + cout << endl; + } + return result; +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + sixth_pattern(num); + return 0; +} +``` + ## Pattern 7 -```js +``` input = 4 * * * * * * * * * * * * * * @@ -819,10 +1045,52 @@ public class Pattern7 { } ``` +## C++ Implementation + +### [seventh_pattern.cpp](./C++/seventh_pattern.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ +#include + +using namespace std; + +void seventh_pattern(int num) { + string result = ""; + int ctr = num, bound = 2 * num + 1; + for (int i = 1; i <= bound - 1; i++) { + for (int j = 1; j <= bound; j++) { + if (j <= ctr || j > bound - ctr) { + cout << "* "; + } else { + cout << " "; + } + } + if (i < num) { + ctr--; + } + if (i > num) { + ctr++; + } + cout << "\n"; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + seventh_pattern(num); + return 0; +} +``` ## Pattern 8 -```js +``` input = 5 * * * * * * @@ -931,3 +1199,46 @@ public class Pattern8 { } } ``` +### C++ Implementation + +## [eighth_solution.cpp](./C++/eighth_solution.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 26/12/2018 + */ + +#include + +using namespace std; + +void eighth_pattern(int num) { + string result = ""; + int bound = 2 * num, ctr = 1; + for (int i = 1; i <= bound; i++) { + for (int j = 1; j <= bound; j++) { + if (j <= ctr || j > bound - ctr) { + cout << "* "; + } else { + cout << " "; + } + } + if (i < num) { + ctr++; + } + if (i > num) { + ctr--; + } + cout << "\n"; + } +} + +int main() { + int num; + cout << "Enter a number: "; + cin >> num; + eighth_pattern(num); + return 0; +} +``` \ No newline at end of file From 543d167c02b1167df83ccb758a14804922412a89 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Thu, 27 Dec 2018 17:12:11 +0800 Subject: [PATCH 21/72] Add solution for Day 6 Problem in Python --- day6/Python/anagram_check.py | 27 ++++++++++++++++++++++++++ day6/Python/reverse_words.py | 14 +++++++++++++ day6/Python/sentence_capitalization.py | 21 ++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 day6/Python/anagram_check.py create mode 100644 day6/Python/reverse_words.py create mode 100644 day6/Python/sentence_capitalization.py diff --git a/day6/Python/anagram_check.py b/day6/Python/anagram_check.py new file mode 100644 index 00000000..5789eec1 --- /dev/null +++ b/day6/Python/anagram_check.py @@ -0,0 +1,27 @@ +''' +@author: aaditkamat +@date: 27/12/2018 +''' + +def check_anagram(first_str, second_str): + first_word_dict = {} + second_word_dict = {} + first_str = first_str.replace(' ', '').lower() + second_str = first_str.replace(' ', '').lower() + for ch in first_str: + if ch not in first_word_dict: + first_word_dict[ch] = 1 + else: + first_word_dict[ch] += 1 + + for ch in second_str: + if ch not in second_word_dict: + second_word_dict[ch] = 1 + else: + second_word_dict[ch] += 1 + return first_word_dict == second_word_dict + +print("Enter two strings: ") +first_str = input() +second_str = input() +print(check_anagram(first_str, second_str)) diff --git a/day6/Python/reverse_words.py b/day6/Python/reverse_words.py new file mode 100644 index 00000000..edefe506 --- /dev/null +++ b/day6/Python/reverse_words.py @@ -0,0 +1,14 @@ +''' +@author: aaditkamat +@date: 27/12/2018 +''' + +def reverse_words(string): + new_string = '' + for word in string.split(' '): + new_string += word[::-1] + ' ' + return new_string + +print("Enter a string: ", end= '') +string = input() +print(reverse_words(string)) diff --git a/day6/Python/sentence_capitalization.py b/day6/Python/sentence_capitalization.py new file mode 100644 index 00000000..cf301823 --- /dev/null +++ b/day6/Python/sentence_capitalization.py @@ -0,0 +1,21 @@ +''' +@author: aaditkamat +@date: 27/12/2018 +''' + +#short version +from string import capwords +def capitalize_sentence_short(string): + return capwords(string) + +#slightly long version +def capitalize_sentence_long(string): + new_string = '' + for word in string.split(' '): + new_string += word.capitalize() + ' ' + return new_string + +print("Enter a string: ", end= ' ') +string = input() +print(capitalize_sentence_short(string)) +print(capitalize_sentence_long(string)) \ No newline at end of file From 9d7d81f88d9f99d447d62883f852caae10432ab5 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Thu, 27 Dec 2018 17:29:53 +0800 Subject: [PATCH 22/72] Update README --- day6/README.md | 81 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/day6/README.md b/day6/README.md index a65cafc8..5002c081 100644 --- a/day6/README.md +++ b/day6/README.md @@ -176,6 +176,33 @@ public class SentenceCap2 { } } ``` +## Python Implementation + +### [sentence_capitalization.py](./Python/sentence_capitalization.py) + +```python +''' +@author: aaditkamat +@date: 27/12/2018 +''' + +#short version +from string import capwords +def capitalize_sentence_short(string): + return capwords(string) + +#slightly long version +def capitalize_sentence_long(string): + new_string = '' + for word in string.split(' '): + new_string += word.capitalize() + ' ' + return new_string + +print("Enter a string: ", end= ' ') +string = input() +print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string)) +print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string)) +``` ## Part B -- Word Reversal @@ -318,6 +345,58 @@ public class WordRev { } ``` +## Python Implementation + +### [reverse_words.py](./Python/reverse_words.py) +```python +''' +@author: aaditkamat +@date: 27/12/2018 +''' + +def reverse_words(string): + new_string = '' + for word in string.split(' '): + new_string += word[::-1] + ' ' + return new_string + +print("Enter a string: ", end= '') +string = input() +print("Reverse of string: ", reverse_words(string)}) +``` ## Part C -- Anagram Check -**Question** - Write a program to check whether the two provided strings are anagrams of each other. \ No newline at end of file +**Question** - Write a program to check whether the two provided strings are anagrams of each other. + +## Python Implementation + +### [reverse_words.py](./Python/reverse_words.py) +```python +''' +@author: aaditkamat +@date: 27/12/2018 +''' + +def check_anagram(first_str, second_str): + first_word_dict = {} + second_word_dict = {} + first_str = first_str.replace(' ', '').lower() + second_str = first_str.replace(' ', '').lower() + for ch in first_str: + if ch not in first_word_dict: + first_word_dict[ch] = 1 + else: + first_word_dict[ch] += 1 + + for ch in second_str: + if ch not in second_word_dict: + second_word_dict[ch] = 1 + else: + second_word_dict[ch] += 1 + return first_word_dict == second_word_dict + +print("Enter two strings: ") +first_str = input() +second_str = input() +print("Are ", first_str, "and ", second_str, "anagrams? ", check_anagram(first_str, second_str)) +``` \ No newline at end of file From 46dbf7b5ecc5a74ce2736e082dccf4ed91d52aae Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Thu, 27 Dec 2018 17:30:14 +0800 Subject: [PATCH 23/72] Refactor code files --- day6/Python/anagram_check.py | 2 +- day6/Python/reverse_words.py | 2 +- day6/Python/sentence_capitalization.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/day6/Python/anagram_check.py b/day6/Python/anagram_check.py index 5789eec1..96d44c6d 100644 --- a/day6/Python/anagram_check.py +++ b/day6/Python/anagram_check.py @@ -24,4 +24,4 @@ def check_anagram(first_str, second_str): print("Enter two strings: ") first_str = input() second_str = input() -print(check_anagram(first_str, second_str)) +print("Are ", first_str, "and ", second_str, "anagrams? ", check_anagram(first_str, second_str)) \ No newline at end of file diff --git a/day6/Python/reverse_words.py b/day6/Python/reverse_words.py index edefe506..34725166 100644 --- a/day6/Python/reverse_words.py +++ b/day6/Python/reverse_words.py @@ -11,4 +11,4 @@ def reverse_words(string): print("Enter a string: ", end= '') string = input() -print(reverse_words(string)) +print("Reverse of string: ", reverse_words(string)) diff --git a/day6/Python/sentence_capitalization.py b/day6/Python/sentence_capitalization.py index cf301823..f834109e 100644 --- a/day6/Python/sentence_capitalization.py +++ b/day6/Python/sentence_capitalization.py @@ -17,5 +17,5 @@ def capitalize_sentence_long(string): print("Enter a string: ", end= ' ') string = input() -print(capitalize_sentence_short(string)) -print(capitalize_sentence_long(string)) \ No newline at end of file +print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string)) +print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string)) \ No newline at end of file From bb3ab0cbd696b3637e54f9a0074efaab56ec8dec Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Thu, 27 Dec 2018 21:07:58 +0800 Subject: [PATCH 24/72] Modify string representation of output in python files --- day6/Python/reverse_words.py | 2 +- day6/Python/sentence_capitalization.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/day6/Python/reverse_words.py b/day6/Python/reverse_words.py index 34725166..916c2fc6 100644 --- a/day6/Python/reverse_words.py +++ b/day6/Python/reverse_words.py @@ -11,4 +11,4 @@ def reverse_words(string): print("Enter a string: ", end= '') string = input() -print("Reverse of string: ", reverse_words(string)) +print("Reverse of string \'", string, "\': ", reverse_words(string), sep='') diff --git a/day6/Python/sentence_capitalization.py b/day6/Python/sentence_capitalization.py index f834109e..43deabce 100644 --- a/day6/Python/sentence_capitalization.py +++ b/day6/Python/sentence_capitalization.py @@ -17,5 +17,5 @@ def capitalize_sentence_long(string): print("Enter a string: ", end= ' ') string = input() -print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string)) -print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string)) \ No newline at end of file +print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string), sep='') +print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string), sep='') \ No newline at end of file From d96703eb014390ec9724b324053b59ba3006fb19 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Thu, 27 Dec 2018 21:08:29 +0800 Subject: [PATCH 25/72] Add Ruby solutions for Day 6 problem --- day6/Ruby/anagram_check.rb | 31 ++++++++++++++++++++++++++++ day6/Ruby/reverse_words.rb | 16 ++++++++++++++ day6/Ruby/sentence_capitalization.rb | 16 ++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 day6/Ruby/anagram_check.rb create mode 100644 day6/Ruby/reverse_words.rb create mode 100644 day6/Ruby/sentence_capitalization.rb diff --git a/day6/Ruby/anagram_check.rb b/day6/Ruby/anagram_check.rb new file mode 100644 index 00000000..d09ec127 --- /dev/null +++ b/day6/Ruby/anagram_check.rb @@ -0,0 +1,31 @@ +=begin +@author: aaditkamat +@date: 27/12/2018 +=end + +def check_anagram(first_str, second_str) + first_word_dict = {} + second_word_dict = {} + first_str.gsub!(" ", "").downcase! + second_str.gsub!(" ", "").downcase! + first_str.each_char do |ch| + if first_word_dict.has_key?(ch) + first_word_dict[ch] += 1 + else + first_word_dict[ch] = 1 + end + end + second_str.each_char do |ch| + if second_word_dict.has_key?(ch) + second_word_dict[ch] += 1 + else + second_word_dict[ch] = 1 + end + end + first_word_dict == second_word_dict +end + +puts "Enter two strings: " +first_str = gets().chomp +second_str = gets().chomp +puts "\nAre #{first_str} and #{second_str} anagrams? #{check_anagram(String.new(first_str), String.new(second_str))}" \ No newline at end of file diff --git a/day6/Ruby/reverse_words.rb b/day6/Ruby/reverse_words.rb new file mode 100644 index 00000000..e2e94270 --- /dev/null +++ b/day6/Ruby/reverse_words.rb @@ -0,0 +1,16 @@ +=begin +@author: aaditkamat +@date: 27/12/2018 +=end + +def reverse_words(string) + new_string = '' + string.split(' ').each do |word| + new_string += word.reverse + ' ' + end + new_string +end + +print"Enter a string: " +string = gets().chomp +print"Reverse of string #{string}: #{reverse_words(string)}" diff --git a/day6/Ruby/sentence_capitalization.rb b/day6/Ruby/sentence_capitalization.rb new file mode 100644 index 00000000..df1c05a4 --- /dev/null +++ b/day6/Ruby/sentence_capitalization.rb @@ -0,0 +1,16 @@ +=begin +@author: aaditkamat +@date: 27/12/2018 +=end + +def capitalize_sentence(string) + new_string = '' + string.split(' ').each do |word| + new_string += word.capitalize + ' ' + end + new_string +end + +print"Enter a string: " +string = gets().chomp +puts "String \" #{string} \" with first letter of each word capitalized: #{capitalize_sentence(string)}" \ No newline at end of file From c2c91da4e2a3c0494ab9f4bc7fef696c54e6a58f Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Thu, 27 Dec 2018 21:25:16 +0800 Subject: [PATCH 26/72] Update README for Ruby code --- day6/README.md | 95 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 91 insertions(+), 4 deletions(-) diff --git a/day6/README.md b/day6/README.md index b3dca0ee..614a4be0 100644 --- a/day6/README.md +++ b/day6/README.md @@ -223,8 +223,32 @@ def capitalize_sentence_long(string): print("Enter a string: ", end= ' ') string = input() -print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string)) -print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string)) +print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string), sep='') +print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string), sep='') +``` + +## Ruby Implementation + +### [sentence_capitalization.rb](./Ruby/sentence_capitalization.rb) + +```ruby + +=begin +@author: aaditkamat +@date: 27/12/2018 +=end + +def capitalize_sentence(string) + new_string = '' + string.split(' ').each do |word| + new_string += word.capitalize + ' ' + end + new_string +end + +print"Enter a string: " +string = gets().chomp +puts "String \" #{string} \" with first letter of each word capitalized: #{capitalize_sentence(string)}" ``` ## Part B -- Word Reversal @@ -421,9 +445,32 @@ def reverse_words(string): print("Enter a string: ", end= '') string = input() -print("Reverse of string: ", reverse_words(string)}) +print("Reverse of string \'", string, "\': ", reverse_words(string), sep='') ``` +## Ruby Implementation + +### [reverse_words.rb](./Ruby/reverse_words.rb) + +```ruby + +=begin +@author: aaditkamat +@date: 27/12/2018 +=end + +def reverse_words(string) + new_string = '' + string.split(' ').each do |word| + new_string += word.reverse + ' ' + end + new_string +end + +print"Enter a string: " +string = gets().chomp +print"Reverse of string #{string}: #{reverse_words(string)}" +``` ## Part C -- Anagram Check **Question** - Write a program to check whether the two provided strings are anagrams of each other. @@ -585,7 +632,7 @@ if( sorted(Str1) != sorted(Str2) ): print("not", end=" ") print("anagrams") ``` -### [reverse_words.py](./Python/reverse_words.py) +### [anagram_check.py](./Python/anagram_check.py) ```python ''' @author: aaditkamat @@ -615,3 +662,43 @@ first_str = input() second_str = input() print("Are ", first_str, "and ", second_str, "anagrams? ", check_anagram(first_str, second_str)) ``` + +## Ruby Implementation + +### [anagram_check.rb](./Ruby/anagram_check.rb) + +```ruby + +=begin +@author: aaditkamat +@date: 27/12/2018 +=end + +def check_anagram(first_str, second_str) + first_word_dict = {} + second_word_dict = {} + first_str.gsub!(" ", "").downcase! + second_str.gsub!(" ", "").downcase! + first_str.each_char do |ch| + if first_word_dict.has_key?(ch) + first_word_dict[ch] += 1 + else + first_word_dict[ch] = 1 + end + end + second_str.each_char do |ch| + if second_word_dict.has_key?(ch) + second_word_dict[ch] += 1 + else + second_word_dict[ch] = 1 + end + end + first_word_dict == second_word_dict +end + +puts "Enter two strings: " +first_str = gets().chomp +second_str = gets().chomp +puts "\nAre #{first_str} and #{second_str} anagrams? #{check_anagram(String.new(first_str), String.new(second_str))}" +``` + From 2e855d24c302113cf1d0dda77232e8830fe1bb92 Mon Sep 17 00:00:00 2001 From: Aadit Date: Fri, 28 Dec 2018 21:44:06 +0800 Subject: [PATCH 27/72] Add first version of solutions for Day 7 problem in C++, Java & Ruby --- day7/C++/one_edit_away.cpp | 49 +++++++++++++++++++++++++++++++++++ day7/Java/OneEditAway.java | 52 ++++++++++++++++++++++++++++++++++++++ day7/Ruby/one_edit_away.rb | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 day7/C++/one_edit_away.cpp create mode 100644 day7/Java/OneEditAway.java create mode 100644 day7/Ruby/one_edit_away.rb diff --git a/day7/C++/one_edit_away.cpp b/day7/C++/one_edit_away.cpp new file mode 100644 index 00000000..b89225a7 --- /dev/null +++ b/day7/C++/one_edit_away.cpp @@ -0,0 +1,49 @@ +/* + * @author: aaditkamat + * @date: 28/12/2018 + */ + +#include + +using namespace std; + +bool can_replace_a_character(string str1, string str2) { + if (str1.size() != str2.size()) { + return false; + } + int change = 0; + for (int i = 0; i < str1.size(); i++) { + if (str1[i] != str2[i]) { + change++; + } + } + return change == 1; +} + +bool is_a_substring(string str1, string str2) { + int found = str2.find(str1); + return found != string::npos; +} + +bool can_add_a_character(string str1, string str2) { + return str2.size() - str1.size() == 1 && is_a_substring(str1, str2); +} + +bool can_delete_a_character(string str1, string str2) { + return str1.size() - str2.size() == 1 && is_a_substring(str2, str1); +} + +bool are_one_edit_away(string str1, string str2) { + if (str1 == str2) { + return true; + } + return can_replace_a_character(str1, str2) || can_add_a_character(str1, str2) || can_delete_a_character(str1, str2); +} + +int main() { + string str1, str2; + cout << "Enter two strings: " << endl; + getline(cin, str1); + getline(cin, str2); + cout << "Are \"" << str1 << "\" and \"" << str2 << "\" one edit away? " << are_one_edit_away(str1, str2) << endl; +} \ No newline at end of file diff --git a/day7/Java/OneEditAway.java b/day7/Java/OneEditAway.java new file mode 100644 index 00000000..fb68adf6 --- /dev/null +++ b/day7/Java/OneEditAway.java @@ -0,0 +1,52 @@ +/** + * @author: aaditkamat + * @date: 28/12/2018 + */ +import java.util.Scanner; + +class OneEditAway { + static boolean canReplaceACharacter(String str1, String str2) { + if (str1.length() != str2.length()) { + return false; + } + int change = 0; + for (int i = 0; i < str1.length(); i++) { + if (str1.charAt(i) != str2.charAt(i)) { + change++; + } + } + return change == 1; + } + + static boolean isASubstring(String str1, String str2) { + if (str1.length() == 0) { + return true; + } + int start = str2.indexOf(str1.charAt(0)); + return str2.substring(start).equals(str1); + } + + static boolean canAddACharacter(String str1, String str2) { + return str2.length() - str1.length() == 1 && isASubstring(str1, str2); + } + + static boolean canDeleteACharacter(String str1, String str2) { + return str1.length() - str2.length() == 1 && isASubstring(str2, str1); + } + + static boolean areOneEditAway(String str1, String str2) { + if (str1.equals(str2)) { + return true; + } + return canReplaceACharacter(str1, str2) || canAddACharacter(str1, str2) || canDeleteACharacter(str1, str2); + } + + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + String str1, str2; + System.out.println("Enter two strings: "); + str1 = input.next(); + str2 = input.next(); + System.out.printf("Are \"%s\" and \"%s\" one edit away? %b\n", str1, str2, areOneEditAway(str1, str2)); + } +} \ No newline at end of file diff --git a/day7/Ruby/one_edit_away.rb b/day7/Ruby/one_edit_away.rb new file mode 100644 index 00000000..8e0d65b7 --- /dev/null +++ b/day7/Ruby/one_edit_away.rb @@ -0,0 +1,45 @@ +=begin +@author: aaditkamat +@date: 28/12/2018 +=end + +def can_replace_a_character(str1, str2) + if (str1.size() != str2.size()) + return false + end + change = 0; + (str1.size()).times do |i| + if (not str1[i] === str2[i]) + change += 1 + end + end + return change === 1 +end + +def is_a_substring(str1, str2) + str2[str1] === str1 +end + +def can_add_a_character(str1, str2) + str2.size() - str1.size() === 1 and is_a_substring(str1, str2) +end + +def can_delete_a_character(str1, str2) + str1.size() - str2.size() === 1 and is_a_substring(str2, str1) +end + +def are_one_edit_away(str1, str2) + if (str1 === str2) + return true + end + can_replace_a_character(str1, str2) or can_add_a_character(str1, str2) or can_delete_a_character(str1, str2) +end + +def main + puts "Enter two strings: " + str1 = gets().chomp! + str2 = gets().chomp! + puts "Are \"#{str1}\" and \"#{str2}\" one edit away? #{are_one_edit_away(str1, str2)}"; +end + +main \ No newline at end of file From d9a1e8ce90beb4a6aebaf0151460952112305800 Mon Sep 17 00:00:00 2001 From: Aadit Date: Fri, 28 Dec 2018 22:37:23 +0800 Subject: [PATCH 28/72] Modify solutions --- day7/C++/one_edit_away.cpp | 40 +++++++++++++++++++++++++++++++++----- day7/Java/OneEditAway.java | 30 +++++++++++++++++++++++++--- day7/Ruby/one_edit_away.rb | 38 +++++++++++++++++++++++++++++++++--- 3 files changed, 97 insertions(+), 11 deletions(-) diff --git a/day7/C++/one_edit_away.cpp b/day7/C++/one_edit_away.cpp index b89225a7..6ddbe60e 100644 --- a/day7/C++/one_edit_away.cpp +++ b/day7/C++/one_edit_away.cpp @@ -11,26 +11,56 @@ bool can_replace_a_character(string str1, string str2) { if (str1.size() != str2.size()) { return false; } + int change = 0; + for (int i = 0; i < str1.size(); i++) { if (str1[i] != str2[i]) { change++; } } + return change == 1; } -bool is_a_substring(string str1, string str2) { - int found = str2.find(str1); - return found != string::npos; +bool is_a_modified_substring(string str1, string str2) { + if (str1.empty()) { + return true; + } + + if (str1[0] != str2[1] && str1[0] != str2[0]) { + return false; + } + + int found = str2.find(str1[0]); + + if (found == 1) { + return str2.find(str1) != string::npos; + } + + int j = 0, ctr = 0; + for (int i = 0; i < str1.size(); j++) { + if (j >= str2.size()) { + return false; + } + if (str1[i] != str2[j] && ctr == 0) { + ctr++; + continue; + } + if (str1[i] != str2[j]) { + return false; + } + i++; + } + return true; } bool can_add_a_character(string str1, string str2) { - return str2.size() - str1.size() == 1 && is_a_substring(str1, str2); + return str2.size() - str1.size() == 1 && is_a_modified_substring(str1, str2); } bool can_delete_a_character(string str1, string str2) { - return str1.size() - str2.size() == 1 && is_a_substring(str2, str1); + return str1.size() - str2.size() == 1 && is_a_modified_substring(str2, str1); } bool are_one_edit_away(string str1, string str2) { diff --git a/day7/Java/OneEditAway.java b/day7/Java/OneEditAway.java index fb68adf6..688649db 100644 --- a/day7/Java/OneEditAway.java +++ b/day7/Java/OneEditAway.java @@ -19,11 +19,35 @@ static boolean canReplaceACharacter(String str1, String str2) { } static boolean isASubstring(String str1, String str2) { - if (str1.length() == 0) { + if (str1.isEmpty()) { return true; } - int start = str2.indexOf(str1.charAt(0)); - return str2.substring(start).equals(str1); + + if (str1.charAt(0) != str2.charAt(1) && str1.charAt(0) != str2.charAt(0)) { + return false; + } + + int found = str2.indexOf(str1.charAt(0)); + + if (found == 1) { + return str2.substring(1).equals(str1); + } + + int j = 0, ctr = 0; + for (int i = 0; i < str1.length(); j++) { + if (j >= str2.length()) { + return false; + } + if (str1.charAt(i) != str2.charAt(j) && ctr == 0) { + ctr++; + continue; + } + if (str1.charAt(i) != str2.charAt(j)) { + return false; + } + i++; + } + return true; } static boolean canAddACharacter(String str1, String str2) { diff --git a/day7/Ruby/one_edit_away.rb b/day7/Ruby/one_edit_away.rb index 8e0d65b7..d2d09a10 100644 --- a/day7/Ruby/one_edit_away.rb +++ b/day7/Ruby/one_edit_away.rb @@ -7,7 +7,7 @@ def can_replace_a_character(str1, str2) if (str1.size() != str2.size()) return false end - change = 0; + change = 0 (str1.size()).times do |i| if (not str1[i] === str2[i]) change += 1 @@ -17,7 +17,39 @@ def can_replace_a_character(str1, str2) end def is_a_substring(str1, str2) - str2[str1] === str1 + if (str1.empty?) + return true + end + + if (not str1[0] === str2[1] and not str1[0] === str2[0]) + return false + end + + found = str2.index(str1[0]) + + if (found == 1) + return str2[1..str2.length][str1] === str1 + end + + i = 0 + j = 0 + ctr = 0 + until i >= str1.length do + if j >= str2.length + return false + end + if (not str1[i] === str2[j] and ctr === 0) + ctr += 1 + j += 1 + next + end + if (not str1[i] === str2[j]) + return false + end + i += 1 + j += 1 + end + true end def can_add_a_character(str1, str2) @@ -39,7 +71,7 @@ def main puts "Enter two strings: " str1 = gets().chomp! str2 = gets().chomp! - puts "Are \"#{str1}\" and \"#{str2}\" one edit away? #{are_one_edit_away(str1, str2)}"; + puts "Are \"#{str1}\" and \"#{str2}\" one edit away? #{are_one_edit_away(str1, str2)}" end main \ No newline at end of file From 5499b34300fa6e18a192c57df1a0e4144f41144e Mon Sep 17 00:00:00 2001 From: Aadit Date: Fri, 28 Dec 2018 22:44:57 +0800 Subject: [PATCH 29/72] Update Day 7 README --- day7/README.md | 250 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) diff --git a/day7/README.md b/day7/README.md index 62ccea5a..81019cf8 100644 --- a/day7/README.md +++ b/day7/README.md @@ -240,3 +240,253 @@ int main() } ``` +### [Solution 2](./C++/one_edit_away.cpp) + +```cpp +/* + * @author: aaditkamat + * @date: 28/12/2018 + */ + +#include + +using namespace std; + +bool can_replace_a_character(string str1, string str2) { + if (str1.size() != str2.size()) { + return false; + } + + int change = 0; + + for (int i = 0; i < str1.size(); i++) { + if (str1[i] != str2[i]) { + change++; + } + } + + return change == 1; +} + +bool is_a_modified_substring(string str1, string str2) { + if (str1.empty()) { + return true; + } + + if (str1[0] != str2[1] && str1[0] != str2[0]) { + return false; + } + + int found = str2.find(str1[0]); + + if (found == 1) { + return str2.find(str1) != string::npos; + } + + int j = 0, ctr = 0; + for (int i = 0; i < str1.size(); j++) { + if (j >= str2.size()) { + return false; + } + if (str1[i] != str2[j] && ctr == 0) { + ctr++; + continue; + } + if (str1[i] != str2[j]) { + return false; + } + i++; + } + return true; +} + +bool can_add_a_character(string str1, string str2) { + return str2.size() - str1.size() == 1 && is_a_modified_substring(str1, str2); +} + +bool can_delete_a_character(string str1, string str2) { + return str1.size() - str2.size() == 1 && is_a_modified_substring(str2, str1); +} + +bool are_one_edit_away(string str1, string str2) { + if (str1 == str2) { + return true; + } + return can_replace_a_character(str1, str2) || can_add_a_character(str1, str2) || can_delete_a_character(str1, str2); +} + +int main() { + string str1, str2; + cout << "Enter two strings: " << endl; + getline(cin, str1); + getline(cin, str2); + cout << "Are \"" << str1 << "\" and \"" << str2 << "\" one edit away? " << are_one_edit_away(str1, str2) << endl; +} +``` + +## Java Implementation + +### [Solution](./Java/OneEditAway.java) + +```java +/** + * @author: aaditkamat + * @date: 28/12/2018 + */ +import java.util.Scanner; + +class OneEditAway { + static boolean canReplaceACharacter(String str1, String str2) { + if (str1.length() != str2.length()) { + return false; + } + int change = 0; + for (int i = 0; i < str1.length(); i++) { + if (str1.charAt(i) != str2.charAt(i)) { + change++; + } + } + return change == 1; + } + + static boolean isASubstring(String str1, String str2) { + if (str1.isEmpty()) { + return true; + } + + if (str1.charAt(0) != str2.charAt(1) && str1.charAt(0) != str2.charAt(0)) { + return false; + } + + int found = str2.indexOf(str1.charAt(0)); + + if (found == 1) { + return str2.substring(1).equals(str1); + } + + int j = 0, ctr = 0; + for (int i = 0; i < str1.length(); j++) { + if (j >= str2.length()) { + return false; + } + if (str1.charAt(i) != str2.charAt(j) && ctr == 0) { + ctr++; + continue; + } + if (str1.charAt(i) != str2.charAt(j)) { + return false; + } + i++; + } + return true; + } + + static boolean canAddACharacter(String str1, String str2) { + return str2.length() - str1.length() == 1 && isASubstring(str1, str2); + } + + static boolean canDeleteACharacter(String str1, String str2) { + return str1.length() - str2.length() == 1 && isASubstring(str2, str1); + } + + static boolean areOneEditAway(String str1, String str2) { + if (str1.equals(str2)) { + return true; + } + return canReplaceACharacter(str1, str2) || canAddACharacter(str1, str2) || canDeleteACharacter(str1, str2); + } + + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + String str1, str2; + System.out.println("Enter two strings: "); + str1 = input.next(); + str2 = input.next(); + System.out.printf("Are \"%s\" and \"%s\" one edit away? %b\n", str1, str2, areOneEditAway(str1, str2)); + } +} +``` + +## Ruby Implementation + +### [Solution](./Ruby/one_edit_away.rb) + +```ruby +=begin +@author: aaditkamat +@date: 28/12/2018 +=end + +def can_replace_a_character(str1, str2) + if (str1.size() != str2.size()) + return false + end + change = 0 + (str1.size()).times do |i| + if (not str1[i] === str2[i]) + change += 1 + end + end + return change === 1 +end + +def is_a_substring(str1, str2) + if (str1.empty?) + return true + end + + if (not str1[0] === str2[1] and not str1[0] === str2[0]) + return false + end + + found = str2.index(str1[0]) + + if (found == 1) + return str2[1..str2.length][str1] === str1 + end + + i = 0 + j = 0 + ctr = 0 + until i >= str1.length do + if j >= str2.length + return false + end + if (not str1[i] === str2[j] and ctr === 0) + ctr += 1 + j += 1 + next + end + if (not str1[i] === str2[j]) + return false + end + i += 1 + j += 1 + end + true +end + +def can_add_a_character(str1, str2) + str2.size() - str1.size() === 1 and is_a_substring(str1, str2) +end + +def can_delete_a_character(str1, str2) + str1.size() - str2.size() === 1 and is_a_substring(str2, str1) +end + +def are_one_edit_away(str1, str2) + if (str1 === str2) + return true + end + can_replace_a_character(str1, str2) or can_add_a_character(str1, str2) or can_delete_a_character(str1, str2) +end + +def main + puts "Enter two strings: " + str1 = gets().chomp! + str2 = gets().chomp! + puts "Are \"#{str1}\" and \"#{str2}\" one edit away? #{are_one_edit_away(str1, str2)}" +end + +main +``` \ No newline at end of file From 96b645f7c8479d715474b57a45ecf561d4b44598 Mon Sep 17 00:00:00 2001 From: Aadit Date: Fri, 28 Dec 2018 22:55:19 +0800 Subject: [PATCH 30/72] Remove merge conflicts from CONTRIBUTORS.md --- CONTRIBUTORS.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 04f086a4..a4f50274 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,12 +1,3 @@ -<<<<<<< HEAD - -[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors) - - -======= -[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors) - ->>>>>>> c2c91da4e2a3c0494ab9f4bc7fef696c54e6a58f ## Contributors Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): From 815d24e7b61c90bd00eb4f90a35499e959fc9c44 Mon Sep 17 00:00:00 2001 From: Aadit Date: Fri, 28 Dec 2018 22:56:22 +0800 Subject: [PATCH 31/72] Add back removed lines in CONTRIBUTORS.md --- CONTRIBUTORS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a4f50274..4a86c1a8 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,3 +1,5 @@ +[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors) + ## Contributors Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): From 38724d26e3d67c6b316255c329c69cbbddc959ab Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sat, 29 Dec 2018 21:05:50 +0800 Subject: [PATCH 32/72] Add code sections contributed by @imkaka to day 6 README --- day6/README.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/day6/README.md b/day6/README.md index 54608fc9..dfd3ba18 100644 --- a/day6/README.md +++ b/day6/README.md @@ -226,6 +226,7 @@ string = input() print("String \'", string, "\' with first letter of each word capitalized (short version): ", capitalize_sentence_short(string)) print("String \'", string, "\' with first letter of each word capitalized (slightly long version): ", capitalize_sentence_long(string)) ``` + ## C++ Implementation ### [Solution](./cpp/Capialise.cpp) @@ -255,6 +256,36 @@ int main() { } ``` +### [Sentance Capatalization by @imkaka](./C++/sentenceCapatilization.cpp) + +```cpp + +/* +* @author : imkaka +* @date : 27/12/2018 +*/ + +#include +#include + +using namespace std; + +int main(){ + string str; + getline(cin, str); + + str[0] = toupper(str[0]); + for(int i = 1; i < str.size(); ++i){ + if(str[i-1] == ' '){ + str[i] = toupper(str[i]); + } + } + + cout << "Result: " << str; + return 0; +} +``` + ## Ruby Implementation ### [sentence_capitalization.rb](./Ruby/sentence_capitalization.rb) @@ -542,6 +573,44 @@ int main() } ``` +### [Word Reverse by @imkaka](./C++/wordReverse.cpp) + +```cpp +/* +* @author : imkaka +* @date : 27/12/2018 +*/ + +#include +#include +#include +#include + +using namespace std; + +int main(){ + string str; + getline(cin, str); + + stringstream ss(str); + string res = ""; + do { + // Read a word + string word; + ss >> word; + + reverse(word.begin(), word.end()); + res += word; + res+=" "; + + // While there is more to read + } while (ss); + + cout << "Reversed Word Sentance => { " << res << " }" << endl; + return 0; +} +``` + ## Ruby Implementation ### [reverse_words.rb](./Ruby/reverse_words.rb) From 0cc23ba9cc2211f367d64f88a0c6b9ff3d194b17 Mon Sep 17 00:00:00 2001 From: MADHAV BAHL Date: Sat, 29 Dec 2018 18:47:04 +0530 Subject: [PATCH 33/72] Update README.md --- day6/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/day6/README.md b/day6/README.md index dfd3ba18..05a86e97 100644 --- a/day6/README.md +++ b/day6/README.md @@ -256,6 +256,37 @@ int main() { } ``` +### [Solution](./C++/day6_1.cpp) + +```cpp +/* +* @author : dhruv-gupta14 +* @date : 27/12/2018 +*/ +#include +using namespace std; +int main() +{ + string s; + getline(cin,s); + + string ans; + ans = toupper(s[0]); + for(int i=1; i < s.size(); i++) + { + if(s[i-1] == ' ') + { + ans += toupper(s[i]); + } else{ + ans += s[i]; + } + } + + cout << ans; + return 0; +} +``` + ### [Sentance Capatalization by @imkaka](./C++/sentenceCapatilization.cpp) ```cpp From 3ddedb449a0793508efa17cbee8359a4568b7189 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 31 Dec 2018 16:03:21 +0800 Subject: [PATCH 34/72] Add C++ solution --- day8/C++/levenshtein_distance.cpp | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 day8/C++/levenshtein_distance.cpp diff --git a/day8/C++/levenshtein_distance.cpp b/day8/C++/levenshtein_distance.cpp new file mode 100644 index 00000000..5c58b542 --- /dev/null +++ b/day8/C++/levenshtein_distance.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + +int ind(int m, int n) { + return m == n; +} + +int dist(string first, string second) { + int firstLength = first.length(), secondLength = second.length(); + int m[firstLength + 1][secondLength + 1]; + for (int i = 1; i <= firstLength; i++) { + m[i][0] = i; + } + for (int i = 0; i <= secondLength; i++) { + m[0][i] = i; + } + for (int i = 1; i <= firstLength; i++) { + for (int j = 1; j <= secondLength; j++) { + int values[] = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i], second[j]) }; + m[i][j] = *min_element(begin(values), end(values)); + } + } + return m[firstLength][secondLength]; +} +int main() { + string first, second; + cout << "Enter two strings: " << endl; + getline(cin, first); + getline(cin, second); + cout << "The Levenshtein distance between \"" << first << "\" and \"" << second << "\" is: " << dist(first, second) << endl; +} From b84a4d4a62ac19967506df558a10c77b42887915 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 31 Dec 2018 16:37:51 +0800 Subject: [PATCH 35/72] Add Day 8 solution in C++ --- day8/C++/levenshtein_distance.cpp | 2 +- day8/Java/LevenshteinDistance.java | 35 ++++++++++++++++++++++++++++++ day8/Ruby/levenshtein_distance.rb | 34 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 day8/Java/LevenshteinDistance.java create mode 100644 day8/Ruby/levenshtein_distance.rb diff --git a/day8/C++/levenshtein_distance.cpp b/day8/C++/levenshtein_distance.cpp index 5c58b542..6ca2c094 100644 --- a/day8/C++/levenshtein_distance.cpp +++ b/day8/C++/levenshtein_distance.cpp @@ -4,7 +4,7 @@ using namespace std; -int ind(int m, int n) { +int ind(char m, char n) { return m == n; } diff --git a/day8/Java/LevenshteinDistance.java b/day8/Java/LevenshteinDistance.java new file mode 100644 index 00000000..e55b8aac --- /dev/null +++ b/day8/Java/LevenshteinDistance.java @@ -0,0 +1,35 @@ +import java.util.Arrays; + +class LevenshteinDistance { + public static int ind(int m, int n) { + return m == n; + } + + public static int dist(String first, String second) { + int firstLength = first.length(), secondLength = second.length(); + int m[firstLength + 1][secondLength + 1]; + for (int i = 1; i <= firstLength; i++) { + m[i][0] = i; + } + for (int i = 0; i <= secondLength; i++) { + m[0][i] = i; + } + for (int i = 1; i <= firstLength; i++) { + for (int j = 1; j <= secondLength; j++) { + int[] values = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i], second[j]) }; + Arrays.sort(values); + m[i][j] = values[0]; + } + } + return m[firstLength][secondLength]; + } + public static void main(String[] args) { + String first, second; + Scanner input = new Scanner(System.in); + System.out.println("Enter two strings: "); + first = input.next(); + second = input.next(); + System.out.printf("The Levenshtein distance between \"%s\" and \"%s\" is: %i\n", first, second, dist(first, second)); + } +} + diff --git a/day8/Ruby/levenshtein_distance.rb b/day8/Ruby/levenshtein_distance.rb new file mode 100644 index 00000000..5c58b542 --- /dev/null +++ b/day8/Ruby/levenshtein_distance.rb @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + +int ind(int m, int n) { + return m == n; +} + +int dist(string first, string second) { + int firstLength = first.length(), secondLength = second.length(); + int m[firstLength + 1][secondLength + 1]; + for (int i = 1; i <= firstLength; i++) { + m[i][0] = i; + } + for (int i = 0; i <= secondLength; i++) { + m[0][i] = i; + } + for (int i = 1; i <= firstLength; i++) { + for (int j = 1; j <= secondLength; j++) { + int values[] = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i], second[j]) }; + m[i][j] = *min_element(begin(values), end(values)); + } + } + return m[firstLength][secondLength]; +} +int main() { + string first, second; + cout << "Enter two strings: " << endl; + getline(cin, first); + getline(cin, second); + cout << "The Levenshtein distance between \"" << first << "\" and \"" << second << "\" is: " << dist(first, second) << endl; +} From 26f256c9273e29e5fd1cff155bfaa7a2b3f3f6c3 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 31 Dec 2018 16:38:09 +0800 Subject: [PATCH 36/72] Add Day 8 solution in Java --- day8/Java/LevenshteinDistance.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/day8/Java/LevenshteinDistance.java b/day8/Java/LevenshteinDistance.java index e55b8aac..b5bb1aa4 100644 --- a/day8/Java/LevenshteinDistance.java +++ b/day8/Java/LevenshteinDistance.java @@ -1,13 +1,14 @@ import java.util.Arrays; +import java.util.Scanner; class LevenshteinDistance { - public static int ind(int m, int n) { - return m == n; + public static int ind(char m, char n) { + return m == n ? 0 : 1; } public static int dist(String first, String second) { int firstLength = first.length(), secondLength = second.length(); - int m[firstLength + 1][secondLength + 1]; + int[][] m = new int[firstLength + 1][secondLength + 1]; for (int i = 1; i <= firstLength; i++) { m[i][0] = i; } @@ -16,7 +17,7 @@ public static int dist(String first, String second) { } for (int i = 1; i <= firstLength; i++) { for (int j = 1; j <= secondLength; j++) { - int[] values = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i], second[j]) }; + int[] values = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first.charAt(i - 1), second.charAt(j - 1)) }; Arrays.sort(values); m[i][j] = values[0]; } @@ -29,7 +30,7 @@ public static void main(String[] args) { System.out.println("Enter two strings: "); first = input.next(); second = input.next(); - System.out.printf("The Levenshtein distance between \"%s\" and \"%s\" is: %i\n", first, second, dist(first, second)); + System.out.printf("The Levenshtein distance between \"%s\" and \"%s\" is: %d\n", first, second, dist(first, second)); } } From 905596d7088d5da67918b88b904d56aa5b542ef3 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 31 Dec 2018 16:38:52 +0800 Subject: [PATCH 37/72] Add Day 8 solution in Ruby --- day8/Ruby/levenshtein_distance.rb | 59 +++++++++++++++---------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/day8/Ruby/levenshtein_distance.rb b/day8/Ruby/levenshtein_distance.rb index 5c58b542..98eb4416 100644 --- a/day8/Ruby/levenshtein_distance.rb +++ b/day8/Ruby/levenshtein_distance.rb @@ -1,34 +1,33 @@ -#include -#include -#include +def ind (m, n) + m == n ? 0 : 1 +end -using namespace std; +def dist(first, second) + first_length = first.length + second_length = second.length -int ind(int m, int n) { - return m == n; -} - -int dist(string first, string second) { - int firstLength = first.length(), secondLength = second.length(); - int m[firstLength + 1][secondLength + 1]; - for (int i = 1; i <= firstLength; i++) { + m = Array.new(first_length + 1) { Array.new(second_length + 1) } + (first.length + 1).times do |i| m[i][0] = i; - } - for (int i = 0; i <= secondLength; i++) { + end + (second_length + 1).times do |i| m[0][i] = i; - } - for (int i = 1; i <= firstLength; i++) { - for (int j = 1; j <= secondLength; j++) { - int values[] = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i], second[j]) }; - m[i][j] = *min_element(begin(values), end(values)); - } - } - return m[firstLength][secondLength]; -} -int main() { - string first, second; - cout << "Enter two strings: " << endl; - getline(cin, first); - getline(cin, second); - cout << "The Levenshtein distance between \"" << first << "\" and \"" << second << "\" is: " << dist(first, second) << endl; -} + end + (1..first_length).each do |i| + (1..second_length).each do |j| + values = [m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i - 1], second[j - 1])]; + values.sort! + m[i][j] = values[0] + end + end + m[first_length][second_length] +end + +def main + puts "Enter two strings: " + first = gets.chomp! + second = gets.chomp! + puts "The Levenshtein distance between \"#{first}\" and \"#{second}\" is: #{dist(first, second)}" +end + +main From a457d3c5fdb3d9d31c3c819acc305040e309961b Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 31 Dec 2018 16:54:31 +0800 Subject: [PATCH 38/72] Add Day 8 solution in Python --- day8/Python/levenshtein_distance.py | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 day8/Python/levenshtein_distance.py diff --git a/day8/Python/levenshtein_distance.py b/day8/Python/levenshtein_distance.py new file mode 100644 index 00000000..df3bd266 --- /dev/null +++ b/day8/Python/levenshtein_distance.py @@ -0,0 +1,35 @@ +def ind (m, n): + return 0 if m == n else 1 + + +def dist(first, second): + first_length = len(first) + second_length = len(second) + + m = [] + for i in range(first_length + 1): + row = [] + for j in range(first_length + 1): + row.append(0) + m.append(row) + + for i in range(first_length + 1): + m[i].append(i); + + for i in range(second_length + 1): + m[0][i] = i; + + for i in range(1, first_length + 1): + for j in range(1, second_length + 1): + values = [m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i - 1], second[j - 1])] + values.sort() + m[i][j] = values[0] + return m[first_length][second_length] + +def main(): + print('Enter two strings: ') + first = input() + second = input() + print(f'The Levenshtein distance between \"{first}\" and \"{second}\" is: {dist(first, second)}') + +main() \ No newline at end of file From f1a18c315f930caaab9c712822ea3ed2cfe74115 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 31 Dec 2018 17:03:24 +0800 Subject: [PATCH 39/72] Add credits at the top of the code --- day8/C++/levenshtein_distance.cpp | 5 +++++ day8/Java/LevenshteinDistance.java | 5 +++++ day8/Python/levenshtein_distance.py | 5 +++++ day8/Ruby/levenshtein_distance.rb | 4 ++++ 4 files changed, 19 insertions(+) diff --git a/day8/C++/levenshtein_distance.cpp b/day8/C++/levenshtein_distance.cpp index 6ca2c094..9f7c5b25 100644 --- a/day8/C++/levenshtein_distance.cpp +++ b/day8/C++/levenshtein_distance.cpp @@ -1,3 +1,8 @@ +/** + * @author: aaditkamat + * @date: 31/12/2018 + */ + #include #include #include diff --git a/day8/Java/LevenshteinDistance.java b/day8/Java/LevenshteinDistance.java index b5bb1aa4..57a2188d 100644 --- a/day8/Java/LevenshteinDistance.java +++ b/day8/Java/LevenshteinDistance.java @@ -1,3 +1,8 @@ +/** + * @author: aaditkamat + * @date: 31/12/2018 + */ + import java.util.Arrays; import java.util.Scanner; diff --git a/day8/Python/levenshtein_distance.py b/day8/Python/levenshtein_distance.py index df3bd266..1b57f887 100644 --- a/day8/Python/levenshtein_distance.py +++ b/day8/Python/levenshtein_distance.py @@ -1,3 +1,8 @@ +''' +@author: aaditkamat +@date: 31/12/2018 +''' + def ind (m, n): return 0 if m == n else 1 diff --git a/day8/Ruby/levenshtein_distance.rb b/day8/Ruby/levenshtein_distance.rb index 98eb4416..041cf818 100644 --- a/day8/Ruby/levenshtein_distance.rb +++ b/day8/Ruby/levenshtein_distance.rb @@ -1,3 +1,7 @@ +=begin +@author: aaditkamat +@date: 31/12/2018 +=end def ind (m, n) m == n ? 0 : 1 end From a283abf067df1f628f58bc3f7db74ae56d12c960 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 31 Dec 2018 17:03:42 +0800 Subject: [PATCH 40/72] Update README --- day8/README.md | 186 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 185 insertions(+), 1 deletion(-) diff --git a/day8/README.md b/day8/README.md index da861444..ebe9c5bc 100644 --- a/day8/README.md +++ b/day8/README.md @@ -97,4 +97,188 @@ function minEditDist (str1, str2) { console.log(minEditDist ('abcdefgs', 'agced')); console.log(minEditDist('kitten', 'sitting')); -``` \ No newline at end of file +``` + +## C++ Implementation + +### [Solution] (./C++/levenshtein_distance.cpp) + +```cpp +/** + * @author: aaditkamat + * @date: 31/12/2018 + */ + +#include +#include +#include + +using namespace std; + +int ind(char m, char n) { + return m == n; +} + +int dist(string first, string second) { + int firstLength = first.length(), secondLength = second.length(); + int m[firstLength + 1][secondLength + 1]; + for (int i = 1; i <= firstLength; i++) { + m[i][0] = i; + } + for (int i = 0; i <= secondLength; i++) { + m[0][i] = i; + } + for (int i = 1; i <= firstLength; i++) { + for (int j = 1; j <= secondLength; j++) { + int values[] = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i], second[j]) }; + m[i][j] = *min_element(begin(values), end(values)); + } + } + return m[firstLength][secondLength]; +} +int main() { + string first, second; + cout << "Enter two strings: " << endl; + getline(cin, first); + getline(cin, second); + cout << "The Levenshtein distance between \"" << first << "\" and \"" << second << "\" is: " << dist(first, second) << endl; +} +``` + +## Java Implementation + +### [Solution] (./Java/LevenshteinDistance.java) + +```java +/** + * @author: aaditkamat + * @date: 31/12/2018 + */ + +import java.util.Arrays; +import java.util.Scanner; + +class LevenshteinDistance { + public static int ind(char m, char n) { + return m == n ? 0 : 1; + } + + public static int dist(String first, String second) { + int firstLength = first.length(), secondLength = second.length(); + int[][] m = new int[firstLength + 1][secondLength + 1]; + for (int i = 1; i <= firstLength; i++) { + m[i][0] = i; + } + for (int i = 0; i <= secondLength; i++) { + m[0][i] = i; + } + for (int i = 1; i <= firstLength; i++) { + for (int j = 1; j <= secondLength; j++) { + int[] values = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first.charAt(i - 1), second.charAt(j - 1)) }; + Arrays.sort(values); + m[i][j] = values[0]; + } + } + return m[firstLength][secondLength]; + } + public static void main(String[] args) { + String first, second; + Scanner input = new Scanner(System.in); + System.out.println("Enter two strings: "); + first = input.next(); + second = input.next(); + System.out.printf("The Levenshtein distance between \"%s\" and \"%s\" is: %d\n", first, second, dist(first, second)); + } +} +``` + +## Python Implementation + +### [Solution](./Python/levenshtein_distance.py) + +```python +''' +@author: aaditkamat +@date: 31/12/2018 +''' + +def ind (m, n): + return 0 if m == n else 1 + + +def dist(first, second): + first_length = len(first) + second_length = len(second) + + m = [] + for i in range(first_length + 1): + row = [] + for j in range(first_length + 1): + row.append(0) + m.append(row) + + for i in range(first_length + 1): + m[i].append(i); + + for i in range(second_length + 1): + m[0][i] = i; + + for i in range(1, first_length + 1): + for j in range(1, second_length + 1): + values = [m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i - 1], second[j - 1])] + values.sort() + m[i][j] = values[0] + return m[first_length][second_length] + +def main(): + print('Enter two strings: ') + first = input() + second = input() + print(f'The Levenshtein distance between \"{first}\" and \"{second}\" is: {dist(first, second)}') + +main() +``` + +## Ruby Implementation + +### [Solution](./Ruby/levenshtein_distance.rb) + +```ruby +=begin +@author: aaditkamat +@date: 31/12/2018 +=end +def ind (m, n) + m == n ? 0 : 1 +end + +def dist(first, second) + first_length = first.length + second_length = second.length + + m = Array.new(first_length + 1) { Array.new(second_length + 1) } + (first.length + 1).times do |i| + m[i][0] = i; + end + (second_length + 1).times do |i| + m[0][i] = i; + end + (1..first_length).each do |i| + (1..second_length).each do |j| + values = [m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i - 1], second[j - 1])]; + values.sort! + m[i][j] = values[0] + end + end + m[first_length][second_length] +end + +def main + puts "Enter two strings: " + first = gets.chomp! + second = gets.chomp! + puts "The Levenshtein distance between \"#{first}\" and \"#{second}\" is: #{dist(first, second)}" +end + +main +``` From 486b4590201eb8fe9872325bdf9b8e5f2653b0c8 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 31 Dec 2018 17:23:28 +0800 Subject: [PATCH 41/72] Update C++ implementation --- day8/C++/levenshtein_distance.cpp | 4 ++-- day8/README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/day8/C++/levenshtein_distance.cpp b/day8/C++/levenshtein_distance.cpp index 9f7c5b25..f1eb1b3a 100644 --- a/day8/C++/levenshtein_distance.cpp +++ b/day8/C++/levenshtein_distance.cpp @@ -10,7 +10,7 @@ using namespace std; int ind(char m, char n) { - return m == n; + return m == n ? 0 : 1; } int dist(string first, string second) { @@ -24,7 +24,7 @@ int dist(string first, string second) { } for (int i = 1; i <= firstLength; i++) { for (int j = 1; j <= secondLength; j++) { - int values[] = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i], second[j]) }; + int values[] = { m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i - 1], second[j - 1])}; m[i][j] = *min_element(begin(values), end(values)); } } diff --git a/day8/README.md b/day8/README.md index ebe9c5bc..3a5198b9 100644 --- a/day8/README.md +++ b/day8/README.md @@ -116,7 +116,7 @@ console.log(minEditDist('kitten', 'sitting')); using namespace std; int ind(char m, char n) { - return m == n; + return m == n ? 0 : 1; } int dist(string first, string second) { From c4c0f9d0b9e3c82287f113d6e107785c2655f5a9 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 31 Dec 2018 17:35:12 +0800 Subject: [PATCH 42/72] Update Python implementation --- day8/Python/levenshtein_distance.py | 8 +++++--- day8/README.md | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/day8/Python/levenshtein_distance.py b/day8/Python/levenshtein_distance.py index 1b57f887..bfa44fb4 100644 --- a/day8/Python/levenshtein_distance.py +++ b/day8/Python/levenshtein_distance.py @@ -14,12 +14,12 @@ def dist(first, second): m = [] for i in range(first_length + 1): row = [] - for j in range(first_length + 1): + for j in range(second_length + 1): row.append(0) m.append(row) - for i in range(first_length + 1): - m[i].append(i); + for i in range(1, first_length + 1): + m[i][0] = i; for i in range(second_length + 1): m[0][i] = i; @@ -29,6 +29,8 @@ def dist(first, second): values = [m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i - 1], second[j - 1])] values.sort() m[i][j] = values[0] + + return m[first_length][second_length] def main(): diff --git a/day8/README.md b/day8/README.md index 3a5198b9..87d375c0 100644 --- a/day8/README.md +++ b/day8/README.md @@ -213,12 +213,12 @@ def dist(first, second): m = [] for i in range(first_length + 1): row = [] - for j in range(first_length + 1): + for j in range(second_length + 1): row.append(0) m.append(row) - for i in range(first_length + 1): - m[i].append(i); + for i in range(1, first_length + 1): + m[i][0] = i; for i in range(second_length + 1): m[0][i] = i; @@ -228,6 +228,8 @@ def dist(first, second): values = [m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i - 1], second[j - 1])] values.sort() m[i][j] = values[0] + + return m[first_length][second_length] def main(): From 61b941fd13b1e99f2c1d7accb803dd969d44a2e6 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Wed, 2 Jan 2019 13:26:54 +0800 Subject: [PATCH 43/72] Add solution for Day 10: String Permutation Problem in Python Signed-off-by: Aadit Rahul Kamat --- day10/Python/permutations.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 day10/Python/permutations.py diff --git a/day10/Python/permutations.py b/day10/Python/permutations.py new file mode 100644 index 00000000..ef2ccbce --- /dev/null +++ b/day10/Python/permutations.py @@ -0,0 +1,29 @@ +''' +@author: aaditkamat +@date: 02/01/2019 +''' + +def permutations(string): + if (len(string) <= 1): + return [string] + lst = [] + for i in range(len(string)): + substring = '' + for j in range(len(string)): + if j != i: + substring += string[j] + lst.extend(list(set(map(lambda x: string[i] + x, permutations(substring))))) + return lst + + +def printList(string_list): + for string in string_list: + print(string) + +def main(): + print('Enter a string: ') + string = input() + print(f'The permutations of {string} are:') + printList(permutations(string)) + +main() \ No newline at end of file From cee4779eb102c6adb2921dab901da94f5c8c9e7a Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Wed, 2 Jan 2019 13:27:06 +0800 Subject: [PATCH 44/72] Update Day 10 README Signed-off-by: Aadit Rahul Kamat --- day10/README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/day10/README.md b/day10/README.md index 458054df..16263b0d 100644 --- a/day10/README.md +++ b/day10/README.md @@ -27,3 +27,39 @@ output: ```js // To Be Added ``` + +## Python Implementation + +### [Solution](./Python/permutations.py) +```python + +''' +@author: aaditkamat +@date: 02/01/2019 +''' + +def permutations(string): + if (len(string) <= 1): + return [string] + lst = [] + for i in range(len(string)): + substring = '' + for j in range(len(string)): + if j != i: + substring += string[j] + lst.extend(list(set(map(lambda x: string[i] + x, permutations(substring))))) + return lst + + +def printList(string_list): + for string in string_list: + print(string) + +def main(): + print('Enter a string: ') + string = input() + print(f'The permutations of {string} are:') + printList(permutations(string)) + +main() +``` \ No newline at end of file From f97ce937ba71d8dfcac110d7ae230d19f4798dee Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Thu, 3 Jan 2019 13:37:37 +0800 Subject: [PATCH 45/72] Change heading in README and remove empty directory in day 9 Signed-off-by: Aadit Rahul Kamat --- day10/README.md | 2 +- day9/dailycodebase | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 160000 day9/dailycodebase diff --git a/day10/README.md b/day10/README.md index 5bc25c7b..3ce9d61d 100644 --- a/day10/README.md +++ b/day10/README.md @@ -1,6 +1,6 @@ ![cover](./cover.png) -# Day 10 -- Smallest Substring Problem +# Day 10 -- String Permutation Problem **Question** Find all the permutations of the given string diff --git a/day9/dailycodebase b/day9/dailycodebase deleted file mode 160000 index e95480ab..00000000 --- a/day9/dailycodebase +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e95480ab4bde1c11326b1a5b925ec0447e821dbb From f1e7f00fe84aa43717144f44bfdfb2ff8ce52ca4 Mon Sep 17 00:00:00 2001 From: MADHAV BAHL Date: Fri, 4 Jan 2019 02:23:15 +0530 Subject: [PATCH 46/72] Update README.md --- day10/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/day10/README.md b/day10/README.md index 475b0246..19ce25a4 100644 --- a/day10/README.md +++ b/day10/README.md @@ -148,7 +148,7 @@ def printList(string_list): def main(): print('Enter a string: ') string = input() - print(f'The permutations of {string} are:') + print('The permutations of {string} are:') printList(permutations(string)) main() @@ -262,4 +262,4 @@ def main(): printList(permutations(string)) main() -``` \ No newline at end of file +``` From b3ee2883e07c99b6444c2df5d472bca682a5da73 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 7 Jan 2019 14:28:43 +0800 Subject: [PATCH 47/72] Add Ruby solutions for Day 13 --- day13/Ruby/factorial.rb | 31 +++++++++++++++ day13/Ruby/fibonacci.rb | 88 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 day13/Ruby/factorial.rb create mode 100644 day13/Ruby/fibonacci.rb diff --git a/day13/Ruby/factorial.rb b/day13/Ruby/factorial.rb new file mode 100644 index 00000000..c76d0324 --- /dev/null +++ b/day13/Ruby/factorial.rb @@ -0,0 +1,31 @@ +def factorial_recursive(num, result) + if num < 0 + return "Factorial of #{num} is undefined" + end + if num === 0 + return result + end + factorial_recursive(num - 1, result * num) +end + +def factorial_iterative(num) + if num < 0 + return "Factorial of #{num} is undefined" + end + result = 1 + i = 1 + until i > num do + result *= i + i += 1 + end + result +end + +def main + print "Enter an integer: " + num = gets.chomp!.to_i + puts "Factorial of #{num} is: #{factorial_recursive(num, 1)}" + puts "Factorial of #{num} is: #{factorial_iterative(num)}" +end + +main \ No newline at end of file diff --git a/day13/Ruby/fibonacci.rb b/day13/Ruby/fibonacci.rb new file mode 100644 index 00000000..26c7bb74 --- /dev/null +++ b/day13/Ruby/fibonacci.rb @@ -0,0 +1,88 @@ +def fibonacci_recursive_value(num, array, ctr) + if num < 1 + return "Incorrect num #{num} for fibonacci sequence" + end + if num == 1 or num == 2 + array[num - 1] = 1 + return array[num - 1] + end + if array[num - 2] != 0 and array[num - 1] != 0 + return array[num - 2] + array[num - 3] + elsif array[num - 2] != 0 + array[num - 3] = fibonacci_recursive_value(num - 2, array, ctr + 1) + elsif array[num - 3] != 0 + array[num - 2] = fibonacci_recursive_value(num - 1, array, ctr + 1) + else + array[num - 2] = fibonacci_recursive_value(num - 1, array, ctr + 1) + array[num - 3] = fibonacci_recursive_value(num - 2, array, ctr + 1) + end + value = array[num - 2] + array[num - 3] + value +end + +def fibonacci_recursive_sequence(num, arr) + if num < 1 + return fibonacci_recursive_value(num, arr, 0) + end + start = 1 + print "The sequence is: " + until start > num do + if start < num + print"#{fibonacci_recursive_value(start, Array.new(arr), 0)}, " + else + puts "#{fibonacci_recursive_value(start, Array.new(arr), 0)}" + end + start += 1 + end +end + +def fibonacci_iterative_sequence(num) + if num < 1 + return "Incorrect num #{num} for fibonacci sequence" + end + start = 1 + print "The sequence is: " + until start > num do + if start < num + print"#{fibonacci_iterative_value(start)}, " + else + puts "#{fibonacci_iterative_value(start)}" + end + start += 1 + end +end + +def fibonacci_iterative_value(num) + if num < 1 + return "Incorrect num #{num} for fibonacci sequence" + end + if num == 1 || num == 2 + return 1 + end + first = 1 + second = 1 + ctr = 2 + until ctr >= num do + first += second + ctr += 1 + if ctr == num + return first + end + second += first + ctr += 1 + end + second +end + +def main + print("Enter an integer: ") + num = gets.chomp!.to_i + a = [] + a.fill(0, 0, num) + puts "The number at position #{num} of fibonacci sequence: #{fibonacci_recursive_value(num, a, 0)}" + fibonacci_recursive_sequence(num, a) + puts "The number at position #{num} of fibonacci sequence: #{fibonacci_iterative_value(num)}" + fibonacci_iterative_sequence(num) +end + +main \ No newline at end of file From 8df29df096109b5de5562000b6cef3ced90703af Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 7 Jan 2019 14:33:03 +0800 Subject: [PATCH 48/72] Update README --- day13/README.md | 140 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/day13/README.md b/day13/README.md index a531c6a1..5eb2f9e9 100644 --- a/day13/README.md +++ b/day13/README.md @@ -118,6 +118,47 @@ int main() *** +## Ruby Implementation + +### [Solution](./Ruby/factorial.rb) +```ruby +=begin +@author: aaditkamat +@date: 07/01/2019 +=end + +def factorial_recursive(num, result) + if num < 0 + return "Factorial of #{num} is undefined" + end + if num === 0 + return result + end + factorial_recursive(num - 1, result * num) +end + +def factorial_iterative(num) + if num < 0 + return "Factorial of #{num} is undefined" + end + result = 1 + i = 1 + until i > num do + result *= i + i += 1 + end + result +end + +def main + print "Enter an integer: " + num = gets.chomp!.to_i + puts "Factorial of #{num} is: #{factorial_recursive(num, 1)}" + puts "Factorial of #{num} is: #{factorial_iterative(num)}" +end + +main +``` ## Part B - Fibonacci Series @@ -230,4 +271,103 @@ int main() cout<<"The "< num do + if start < num + print"#{fibonacci_recursive_value(start, Array.new(arr), 0)}, " + else + puts "#{fibonacci_recursive_value(start, Array.new(arr), 0)}" + end + start += 1 + end +end + +def fibonacci_iterative_sequence(num) + if num < 1 + return "Incorrect num #{num} for fibonacci sequence" + end + start = 1 + print "The sequence is: " + until start > num do + if start < num + print"#{fibonacci_iterative_value(start)}, " + else + puts "#{fibonacci_iterative_value(start)}" + end + start += 1 + end +end + +def fibonacci_iterative_value(num) + if num < 1 + return "Incorrect num #{num} for fibonacci sequence" + end + if num == 1 || num == 2 + return 1 + end + first = 1 + second = 1 + ctr = 2 + until ctr >= num do + first += second + ctr += 1 + if ctr == num + return first + end + second += first + ctr += 1 + end + second +end + +def main + print("Enter an integer: ") + num = gets.chomp!.to_i + a = [] + a.fill(0, 0, num) + puts "The number at position #{num} of fibonacci sequence: #{fibonacci_recursive_value(num, a, 0)}" + fibonacci_recursive_sequence(num, a) + puts "The number at position #{num} of fibonacci sequence: #{fibonacci_iterative_value(num)}" + fibonacci_iterative_sequence(num) +end + +main ``` \ No newline at end of file From e9c2cef0f885a6e79bd73d10a95fec40f7aaf48e Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 7 Jan 2019 14:33:16 +0800 Subject: [PATCH 49/72] Add credits to the code --- day13/Ruby/factorial.rb | 5 +++++ day13/Ruby/fibonacci.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/day13/Ruby/factorial.rb b/day13/Ruby/factorial.rb index c76d0324..bf6d829b 100644 --- a/day13/Ruby/factorial.rb +++ b/day13/Ruby/factorial.rb @@ -1,3 +1,8 @@ +=begin +@author: aaditkamat +@date: 07/01/2019 +=end + def factorial_recursive(num, result) if num < 0 return "Factorial of #{num} is undefined" diff --git a/day13/Ruby/fibonacci.rb b/day13/Ruby/fibonacci.rb index 26c7bb74..32cb705b 100644 --- a/day13/Ruby/fibonacci.rb +++ b/day13/Ruby/fibonacci.rb @@ -1,3 +1,8 @@ +=begin +@author: aaditkamat +@date: 07/01/2019 +=end + def fibonacci_recursive_value(num, array, ctr) if num < 1 return "Incorrect num #{num} for fibonacci sequence" From dcc1b87d92c63978f5fd6edb3dd61180bbbfc83c Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 7 Jan 2019 15:05:19 +0800 Subject: [PATCH 50/72] Add Python solution for Day 13 --- day13/Python/factorial.py | 29 +++++++++++++++ day13/Python/fibonacci.py | 75 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 day13/Python/factorial.py create mode 100644 day13/Python/fibonacci.py diff --git a/day13/Python/factorial.py b/day13/Python/factorial.py new file mode 100644 index 00000000..226c43e1 --- /dev/null +++ b/day13/Python/factorial.py @@ -0,0 +1,29 @@ +""" +@author: aaditkamat +@date: 07/01/2019 +""" + +def factorial_recursive(num, result): + if num < 0: + return "Factorial of #{num} is undefined" + if num == 0: + return result + return factorial_recursive(num - 1, result * num) + +def factorial_iterative(num): + if num < 0: + return "Factorial of #{num} is undefined" + result = 1 + i = 1 + while i <= num: + result *= i + i += 1 + return result + +def main(): + print("Enter an integer: ") + num = int(input()) + print(f'Factorial of {num} is: {factorial_recursive(num, 1)}') + print(f'Factorial of {num} is: {factorial_iterative(num)}') + +main() \ No newline at end of file diff --git a/day13/Python/fibonacci.py b/day13/Python/fibonacci.py new file mode 100644 index 00000000..2649b239 --- /dev/null +++ b/day13/Python/fibonacci.py @@ -0,0 +1,75 @@ +""" +@author: aaditkamat +@date: 07/01/2019 +""" + +def fibonacci_recursive_value(num, array, ctr): + if num < 1: + return f'Incorrect num #{num} for fibonacci sequence' + if num == 1 or num == 2: + array[num - 1] = 1 + return array[num - 1] + if array[num - 2] != 0 and array[num - 1] != 0: + return array[num - 2] + array[num - 3] + elif array[num - 2] != 0: + array[num - 3] = fibonacci_recursive_value(num - 2, array, ctr + 1) + elif array[num - 3] != 0: + array[num - 2] = fibonacci_recursive_value(num - 1, array, ctr + 1) + else: + array[num - 2] = fibonacci_recursive_value(num - 1, array, ctr + 1) + array[num - 3] = fibonacci_recursive_value(num - 2, array, ctr + 1) + return array[num - 2] + array[num - 3] + +def fibonacci_recursive_sequence(num, arr): + if num < 1: + return fibonacci_recursive_value(num, arr, 0) + start = 1 + str = 'The sequence is: ' + while start <= num: + if start < num: + str += f'{fibonacci_recursive_value(start, arr[:], 0)}, ' + else: + str += f'{fibonacci_recursive_value(start, arr[:], 0)}' + start += 1 + print(str) + +def fibonacci_iterative_sequence(num): + if num < 1: + return f'Incorrect num {num} for fibonacci sequence' + start = 1 + str = 'The sequence is: ' + while start <= num: + if start < num: + str += f'{fibonacci_iterative_value(start)}, ' + else: + str += f'{fibonacci_iterative_value(start)}' + start += 1 + print(str) + +def fibonacci_iterative_value(num): + if num < 1: + return "Incorrect num #{num} for fibonacci sequence" + if num == 1 or num == 2: + return 1 + first = 1 + second = 1 + ctr = 2 + while ctr < num: + first += second + ctr += 1 + if ctr == num: + return first + second += first + ctr += 1 + return second + +def main(): + print("Enter an integer: ") + num = int(input()) + a = [0] * num + print(f'The number at position {num} of fibonacci sequence: {fibonacci_recursive_value(num, a, 0)}') + fibonacci_recursive_sequence(num, a) + print(f'The number at position {num} of fibonacci sequence: {fibonacci_iterative_value(num)}') + fibonacci_iterative_sequence(num) + +main() \ No newline at end of file From 4ce5d733f3536a6b75d00ff136bef57ac1be75c3 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Mon, 7 Jan 2019 15:05:33 +0800 Subject: [PATCH 51/72] Update Day 13 README --- day13/README.md | 116 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/day13/README.md b/day13/README.md index 5eb2f9e9..cc178a1e 100644 --- a/day13/README.md +++ b/day13/README.md @@ -160,6 +160,41 @@ end main ``` +## Python implementation + +### [Solution](./Python/factorial.py) +```python +""" +@author: aaditkamat +@date: 07/01/2019 +""" + +def factorial_recursive(num, result): + if num < 0: + return "Factorial of #{num} is undefined" + if num == 0: + return result + return factorial_recursive(num - 1, result * num) + +def factorial_iterative(num): + if num < 0: + return "Factorial of #{num} is undefined" + result = 1 + i = 1 + while i <= num: + result *= i + i += 1 + return result + +def main(): + print("Enter an integer: ") + num = int(input()) + print(f'Factorial of {num} is: {factorial_recursive(num, 1)}') + print(f'Factorial of {num} is: {factorial_iterative(num)}') + +main() +``` + ## Part B - Fibonacci Series ### JavaScript Implementation @@ -370,4 +405,85 @@ def main end main +``` + +## Python implementation + +### [Solution](./Python/fibonacci.py) +```python +""" +@author: aaditkamat +@date: 07/01/2019 +""" + +def fibonacci_recursive_value(num, array, ctr): + if num < 1: + return f'Incorrect num #{num} for fibonacci sequence' + if num == 1 or num == 2: + array[num - 1] = 1 + return array[num - 1] + if array[num - 2] != 0 and array[num - 1] != 0: + return array[num - 2] + array[num - 3] + elif array[num - 2] != 0: + array[num - 3] = fibonacci_recursive_value(num - 2, array, ctr + 1) + elif array[num - 3] != 0: + array[num - 2] = fibonacci_recursive_value(num - 1, array, ctr + 1) + else: + array[num - 2] = fibonacci_recursive_value(num - 1, array, ctr + 1) + array[num - 3] = fibonacci_recursive_value(num - 2, array, ctr + 1) + return array[num - 2] + array[num - 3] + +def fibonacci_recursive_sequence(num, arr): + if num < 1: + return fibonacci_recursive_value(num, arr, 0) + start = 1 + str = 'The sequence is: ' + while start <= num: + if start < num: + str += f'{fibonacci_recursive_value(start, arr[:], 0)}, ' + else: + str += f'{fibonacci_recursive_value(start, arr[:], 0)}' + start += 1 + print(str) + +def fibonacci_iterative_sequence(num): + if num < 1: + return f'Incorrect num {num} for fibonacci sequence' + start = 1 + str = 'The sequence is: ' + while start <= num: + if start < num: + str += f'{fibonacci_iterative_value(start)}, ' + else: + str += f'{fibonacci_iterative_value(start)}' + start += 1 + print(str) + +def fibonacci_iterative_value(num): + if num < 1: + return "Incorrect num #{num} for fibonacci sequence" + if num == 1 or num == 2: + return 1 + first = 1 + second = 1 + ctr = 2 + while ctr < num: + first += second + ctr += 1 + if ctr == num: + return first + second += first + ctr += 1 + return second + +def main(): + print("Enter an integer: ") + num = int(input()) + a = [0] * num + print(f'The number at position {num} of fibonacci sequence: {fibonacci_recursive_value(num, a, 0)}') + fibonacci_recursive_sequence(num, a) + print(f'The number at position {num} of fibonacci sequence: {fibonacci_iterative_value(num)}') + fibonacci_iterative_sequence(num) + +main() ``` \ No newline at end of file From 38e180b771cbea1d9920dabd90356d90d3485477 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Mon, 7 Jan 2019 15:33:10 +0800 Subject: [PATCH 52/72] Update fibonacci.py --- day13/Python/fibonacci.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/day13/Python/fibonacci.py b/day13/Python/fibonacci.py index 2649b239..18ccfdc8 100644 --- a/day13/Python/fibonacci.py +++ b/day13/Python/fibonacci.py @@ -24,27 +24,27 @@ def fibonacci_recursive_sequence(num, arr): if num < 1: return fibonacci_recursive_value(num, arr, 0) start = 1 - str = 'The sequence is: ' + string = 'The sequence is: ' while start <= num: if start < num: - str += f'{fibonacci_recursive_value(start, arr[:], 0)}, ' + string += f'{fibonacci_recursive_value(start, arr[:], 0)}, ' else: - str += f'{fibonacci_recursive_value(start, arr[:], 0)}' + string += f'{fibonacci_recursive_value(start, arr[:], 0)}' start += 1 - print(str) + print(string) def fibonacci_iterative_sequence(num): if num < 1: return f'Incorrect num {num} for fibonacci sequence' start = 1 - str = 'The sequence is: ' + string = 'The sequence is: ' while start <= num: if start < num: - str += f'{fibonacci_iterative_value(start)}, ' + string += f'{fibonacci_iterative_value(start)}, ' else: - str += f'{fibonacci_iterative_value(start)}' + string += f'{fibonacci_iterative_value(start)}' start += 1 - print(str) + print(string) def fibonacci_iterative_value(num): if num < 1: @@ -72,4 +72,4 @@ def main(): print(f'The number at position {num} of fibonacci sequence: {fibonacci_iterative_value(num)}') fibonacci_iterative_sequence(num) -main() \ No newline at end of file +main() From 2213514ddac3e921257676216b4f80d283c523f0 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Mon, 7 Jan 2019 15:35:07 +0800 Subject: [PATCH 53/72] Modify Fibonacci python code section in day 13 README --- day13/README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/day13/README.md b/day13/README.md index cc178a1e..70007e18 100644 --- a/day13/README.md +++ b/day13/README.md @@ -437,27 +437,27 @@ def fibonacci_recursive_sequence(num, arr): if num < 1: return fibonacci_recursive_value(num, arr, 0) start = 1 - str = 'The sequence is: ' + string = 'The sequence is: ' while start <= num: if start < num: - str += f'{fibonacci_recursive_value(start, arr[:], 0)}, ' + string += f'{fibonacci_recursive_value(start, arr[:], 0)}, ' else: - str += f'{fibonacci_recursive_value(start, arr[:], 0)}' + string += f'{fibonacci_recursive_value(start, arr[:], 0)}' start += 1 - print(str) + print(string) def fibonacci_iterative_sequence(num): if num < 1: return f'Incorrect num {num} for fibonacci sequence' start = 1 - str = 'The sequence is: ' + string = 'The sequence is: ' while start <= num: if start < num: - str += f'{fibonacci_iterative_value(start)}, ' + string += f'{fibonacci_iterative_value(start)}, ' else: - str += f'{fibonacci_iterative_value(start)}' + string += f'{fibonacci_iterative_value(start)}' start += 1 - print(str) + print(string) def fibonacci_iterative_value(num): if num < 1: @@ -486,4 +486,4 @@ def main(): fibonacci_iterative_sequence(num) main() -``` \ No newline at end of file +``` From 5758d99816ce3d022fc25cf8600213b5f45d9972 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Sat, 12 Jan 2019 00:42:03 +0800 Subject: [PATCH 54/72] Add Ruby solution to Day 16 problem Signed-off-by: Aadit Rahul Kamat --- day16/Ruby/hanoi.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 day16/Ruby/hanoi.rb diff --git a/day16/Ruby/hanoi.rb b/day16/Ruby/hanoi.rb new file mode 100644 index 00000000..f479aee5 --- /dev/null +++ b/day16/Ruby/hanoi.rb @@ -0,0 +1,31 @@ +=begin +@author: aaditkamat +@date: 12/01/2019 +=end + +def hanoi(start_rod, aux_rod, end_rod, num) + if num < 0 + puts "The number of disks must be a non-negative integer" + return + end + if num == 0 + return + end + if num == 1 + puts "Move disk from rod #{start_rod} to rod #{end_rod}" + return + end + hanoi(start_rod, end_rod, aux_rod, num - 1) + hanoi(start_rod, aux_rod, end_rod, 1) + hanoi(aux_rod, start_rod, end_rod, num - 1) +end + +def main + print "Enter number of disks: " + num = gets.chomp!.to_i + print "The sequence of instructions to move #{num} disks where disk 1 is the start rod" + puts "disk 2 is the auxiliary rod and disk 3 is the end rod are as follows: " + hanoi(1, 2, 3, num) +end + +main \ No newline at end of file From 3cf81e54109e643d62ae76a01f37c1ea85652519 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Sat, 12 Jan 2019 00:42:25 +0800 Subject: [PATCH 55/72] Update Day 16 README with Ruby implementation Signed-off-by: Aadit Rahul Kamat --- day16/README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/day16/README.md b/day16/README.md index a2506882..88ec557e 100644 --- a/day16/README.md +++ b/day16/README.md @@ -44,4 +44,42 @@ console.log ('/* ===== for 2 disks ===== */'); towerOfHanoi (2, 'A', 'C', 'B'); console.log ('\n/* ===== for 3 disks ===== */'); towerOfHanoi (3, 'A', 'C', 'B'); +``` + +## Ruby Implementation + +### [Solution](./Ruby/hanoi.rb) + +```ruby +=begin +@author: aaditkamat +@date: 12/01/2019 +=end + +def hanoi(start_rod, aux_rod, end_rod, num) + if num < 0 + puts "The number of disks must be a non-negative integer" + return + end + if num == 0 + return + end + if num == 1 + puts "Move disk from rod #{start_rod} to rod #{end_rod}" + return + end + hanoi(start_rod, end_rod, aux_rod, num - 1) + hanoi(start_rod, aux_rod, end_rod, 1) + hanoi(aux_rod, start_rod, end_rod, num - 1) +end + +def main + print "Enter number of disks: " + num = gets.chomp!.to_i + print "The sequence of instructions to move #{num} disks where disk 1 is the start rod" + puts "disk 2 is the auxiliary rod and disk 3 is the end rod are as follows: " + hanoi(1, 2, 3, num) +end + +main ``` \ No newline at end of file From d4e98e9f526a5f5dcba525118d133a072ad6d263 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Sat, 12 Jan 2019 00:45:22 +0800 Subject: [PATCH 56/72] Change wording of print statement Signed-off-by: Aadit Rahul Kamat --- day16/README.md | 2 +- day16/Ruby/hanoi.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/day16/README.md b/day16/README.md index 88ec557e..7231a336 100644 --- a/day16/README.md +++ b/day16/README.md @@ -65,7 +65,7 @@ def hanoi(start_rod, aux_rod, end_rod, num) return end if num == 1 - puts "Move disk from rod #{start_rod} to rod #{end_rod}" + puts "Move top most disk from rod #{start_rod} to rod #{end_rod}" return end hanoi(start_rod, end_rod, aux_rod, num - 1) diff --git a/day16/Ruby/hanoi.rb b/day16/Ruby/hanoi.rb index f479aee5..af412c49 100644 --- a/day16/Ruby/hanoi.rb +++ b/day16/Ruby/hanoi.rb @@ -12,7 +12,7 @@ def hanoi(start_rod, aux_rod, end_rod, num) return end if num == 1 - puts "Move disk from rod #{start_rod} to rod #{end_rod}" + puts "Move top most disk from rod #{start_rod} to rod #{end_rod}" return end hanoi(start_rod, end_rod, aux_rod, num - 1) From 438887f0d61384579655cea15ca758daed59ac8d Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sun, 13 Jan 2019 23:16:14 +0800 Subject: [PATCH 57/72] Add Python solution to Day 17 problem Signed-off-by: Aadit Rahul Kamat --- day17/Python/n_queens.py | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 day17/Python/n_queens.py diff --git a/day17/Python/n_queens.py b/day17/Python/n_queens.py new file mode 100644 index 00000000..92de18a8 --- /dev/null +++ b/day17/Python/n_queens.py @@ -0,0 +1,80 @@ +""" +@author:aaditkamat +@date: 13/01/2019 +""" +def print_chessboard(chessboard): + for i in range(len(chessboard)): + print(chessboard[i]) + +def fill_chessboard(i, j,chessboard): + chessboard[i][j] = 1 + + for x in range(len(chessboard)): + if x != i: + chessboard[x][j] = 0 + + for y in range(len(chessboard)): + if y != j: + chessboard[i][y] = 0 + + x, y = (i + 1, j + 1) + while x < len(chessboard) and y < len(chessboard): + chessboard[x][y] = 0 + x += 1 + y += 1 + + x, y = (i - 1, j - 1) + while x >= 0 and y >= 0: + chessboard[x][y] = 0 + x -= 1 + y -= 1 + + x, y = (i + 1, j - 1) + while x < len(chessboard) and y >= 0: + chessboard[x][y] = 0 + x += 1 + y -= 1 + + x, y = (i - 1, j + 1) + while x >= 0 and y < len(chessboard): + chessboard[x][y] = 0 + x -= 1 + y += 1 + +def try_position(i, chessboard): + fill_chessboard(0, i, chessboard) + + for i in range(1, len(chessboard)): + for j in range(len(chessboard)): + if chessboard[i][j] == -1: + fill_chessboard(i, j, chessboard) + +def reset_chessboard(chessboard): + for i in range(len(chessboard)): + for j in range(len(chessboard)): + chessboard[i][j] = -1 + +def is_correct(chessboard): + for i in range(len(chessboard)): + if chessboard[i].count(1) == 0: + return False + return True + +def n_queens(num): + chessboard = [] + for i in range(num): + chessboard.append([-1] * num) + + for i in range(num): + try_position(i, chessboard) + if (is_correct(chessboard)): + print_chessboard(chessboard) + return + reset_chessboard(chessboard) + +def main(): + print("Enter number of queens: ", end="") + num = int(input()) + n_queens(num) + +main() \ No newline at end of file From 54f6d4f1c1af79f7e0554a92b550fbf6f5590e3b Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Sun, 13 Jan 2019 23:17:11 +0800 Subject: [PATCH 58/72] Add Python implementation to Day 17 README Signed-off-by: Aadit Rahul Kamat --- day17/README.md | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/day17/README.md b/day17/README.md index 49452695..c1c067a8 100644 --- a/day17/README.md +++ b/day17/README.md @@ -209,3 +209,90 @@ int main() { checkSolution(); } ``` + +## Python Implemetation + +### [Solution](./Python/n_queens.py) + +```python +""" +@author:aaditkamat +@date: 13/01/2019 +""" +def print_chessboard(chessboard): + for i in range(len(chessboard)): + print(chessboard[i]) + +def fill_chessboard(i, j,chessboard): + chessboard[i][j] = 1 + + for x in range(len(chessboard)): + if x != i: + chessboard[x][j] = 0 + + for y in range(len(chessboard)): + if y != j: + chessboard[i][y] = 0 + + x, y = (i + 1, j + 1) + while x < len(chessboard) and y < len(chessboard): + chessboard[x][y] = 0 + x += 1 + y += 1 + + x, y = (i - 1, j - 1) + while x >= 0 and y >= 0: + chessboard[x][y] = 0 + x -= 1 + y -= 1 + + x, y = (i + 1, j - 1) + while x < len(chessboard) and y >= 0: + chessboard[x][y] = 0 + x += 1 + y -= 1 + + x, y = (i - 1, j + 1) + while x >= 0 and y < len(chessboard): + chessboard[x][y] = 0 + x -= 1 + y += 1 + +def try_position(i, chessboard): + fill_chessboard(0, i, chessboard) + + for i in range(1, len(chessboard)): + for j in range(len(chessboard)): + if chessboard[i][j] == -1: + fill_chessboard(i, j, chessboard) + +def reset_chessboard(chessboard): + for i in range(len(chessboard)): + for j in range(len(chessboard)): + chessboard[i][j] = -1 + +def is_correct(chessboard): + for i in range(len(chessboard)): + if chessboard[i].count(1) == 0: + return False + return True + +def n_queens(num): + chessboard = [] + for i in range(num): + chessboard.append([-1] * num) + + for i in range(num): + try_position(i, chessboard) + if (is_correct(chessboard)): + print_chessboard(chessboard) + return + reset_chessboard(chessboard) + +def main(): + print("Enter number of queens: ", end="") + num = int(input()) + n_queens(num) + +main() +``` \ No newline at end of file From d93291b5d75b246b0fc2bfebadce91a96c312a14 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Tue, 15 Jan 2019 16:15:02 +0800 Subject: [PATCH 59/72] Add solution to Day 18 Question A: Frequency Counter --- day18/Python/frequency_counter.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 day18/Python/frequency_counter.py diff --git a/day18/Python/frequency_counter.py b/day18/Python/frequency_counter.py new file mode 100644 index 00000000..62c95c7f --- /dev/null +++ b/day18/Python/frequency_counter.py @@ -0,0 +1,26 @@ +""" +author: @aaditkamat +date: 15/01/2019 +""" +def count_frequencies(lst): + frequency_dictionary = {} + + for num in lst: + if num not in frequency_dictionary: + frequency_dictionary[num] = 1 + else: + frequency_dictionary[num] += 1 + + for key in frequency_dictionary: + print(f'\'{key}\' is present {frequency_dictionary[key]} time(s)') + +def convert_input_array_to_list(input_array): + return list(map(lambda x: int(x), input_array.strip('[').strip(']').split(','))) + +def main(): + print('Enter an input array: ', end='') + input_array = input() + lst = convert_input_array_to_list(input_array) + count_frequencies(lst) + +main() From bb7a8bd4c1180e35125029e078908e6898e35a77 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Tue, 15 Jan 2019 17:19:32 +0800 Subject: [PATCH 60/72] Add solution to Day 18 Question B: Count Uniques --- day18/Python/count_uniques.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 day18/Python/count_uniques.py diff --git a/day18/Python/count_uniques.py b/day18/Python/count_uniques.py new file mode 100644 index 00000000..67d96cfe --- /dev/null +++ b/day18/Python/count_uniques.py @@ -0,0 +1,13 @@ +''' +@author: aaditkamat +@date: 15/01/2019 +''' +from frequency_counter import handle_input, count_frequencies + +def count_uniques(frequency_dictionary): + print(f'Number of unique elements = {len(frequency_dictionary)}') + +def main(): + lst = handle_input('an') + frequency_dictionary = count_frequencies(lst) + count_uniques(frequency_dictionary) From 7eebd0605561c6a0579f50d7bf1b6503e7db8152 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Tue, 15 Jan 2019 17:22:08 +0800 Subject: [PATCH 61/72] Add solution to Day 18 Question C: Check Power N --- day18/Python/check_power_n.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 day18/Python/check_power_n.py diff --git a/day18/Python/check_power_n.py b/day18/Python/check_power_n.py new file mode 100644 index 00000000..8b95af3d --- /dev/null +++ b/day18/Python/check_power_n.py @@ -0,0 +1,24 @@ +''' +@author:aaditkamat +@date: 15/01/2019 +''' +from frequency_counter import handle_input + +""" +I wrote a one liner for the check_power_n function thanks to python's generators which is quite messy ;-) +print(len(list(filter(lambda x: x == 1, map(lambda x: len([num for num in first if x == num ** N]), second)))) == len(second)) +""" +def check_power_n(first, second, N): + count = 0 + for num in first: + for x in second: + if x == num ** N: + count += 1 + print(count == len(second)) + +def main(): + first = handle_input('first') + second = handle_input('second') + print('Enter a number: ', end='') + N = int(input()) + check_power_n(first, second, N) From 37af7b08715b8503d04d1c7d2755389875a1d0bf Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Tue, 15 Jan 2019 17:55:43 +0800 Subject: [PATCH 62/72] Update day 18 README with personal Python solution --- day18/README.md | 86 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/day18/README.md b/day18/README.md index cbf433d9..365d6ad5 100644 --- a/day18/README.md +++ b/day18/README.md @@ -216,6 +216,38 @@ for v,c in count.items(): print("\'" + str(v) + "\' is present " + str(c) + " time(s)") ``` +### [Solution 2](./Python/frequency_counter.py) + +```python + +""" +author: @aaditkamat +date: 15/01/2019 +""" +def count_frequencies(lst): + frequency_dictionary = {} + + for num in lst: + if num not in frequency_dictionary: + frequency_dictionary[num] = 1 + else: + frequency_dictionary[num] += 1 + + for key in frequency_dictionary: + print(f'\'{key}\' is present {frequency_dictionary[key]} time(s)') + +def convert_input_array_to_list(input_array): + return list(map(lambda x: int(x), input_array.strip('[').strip(']').split(','))) + +def main(): + print('Enter an input array: ', end='') + input_array = input() + lst = convert_input_array_to_list(input_array) + count_frequencies(lst) + +main() +``` + *** ## Question 2 -- Count Uniques @@ -434,6 +466,26 @@ data = [1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 7] print("Number of unique elements =", countUniques(data)) ``` +#### [Solution 2](./Python/count_uniques.py) + +```python +''' +@author: aaditkamat +@date: 15/01/2019 +''' +from frequency_counter import handle_input, count_frequencies + +def count_uniques(frequency_dictionary): + print(f'Number of unique elements = {len(frequency_dictionary)}') + +def main(): + lst = handle_input('an') + frequency_dictionary = count_frequencies(lst) + count_uniques(frequency_dictionary) + +main() +``` + *** ## Question 3 -- Check Power N @@ -693,4 +745,36 @@ arr2 = [1, 2, 3] n = 4 print("arr1 =", arr1, "arr2 =", arr2, "n =", n) print("output = ", checkPowerN(arr1, arr2, n)) -``` \ No newline at end of file +``` + +### Python Implementation + +#### [Solution 2](./Python/check_power_n.py) +```python +''' +@author:aaditkamat +@date: 15/01/2019 +''' +from frequency_counter import handle_input + +""" +I wrote a one liner for the check_power_n function thanks to python's generators which is quite messy ;-) +print(len(list(filter(lambda x: x == 1, map(lambda x: len([num for num in first if x == num ** N]), second)))) == len(second)) +""" +def check_power_n(first, second, N): + count = 0 + for num in first: + for x in second: + if x == num ** N: + count += 1 + print(count == len(second)) + +def main(): + first = handle_input('first') + second = handle_input('second') + print('Enter a number: ', end='') + N = int(input()) + check_power_n(first, second, N) + +main() +``` From a660086fdc678116eb1819fc06e07e6ea59cfec0 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Tue, 15 Jan 2019 17:56:23 +0800 Subject: [PATCH 63/72] Update check_power_n.py --- day18/Python/check_power_n.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/day18/Python/check_power_n.py b/day18/Python/check_power_n.py index 8b95af3d..6a32f74f 100644 --- a/day18/Python/check_power_n.py +++ b/day18/Python/check_power_n.py @@ -22,3 +22,5 @@ def main(): print('Enter a number: ', end='') N = int(input()) check_power_n(first, second, N) + + main() From bc1b5bb938b822b4513ec217d2567dffffa1659b Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Tue, 15 Jan 2019 17:57:02 +0800 Subject: [PATCH 64/72] Update count_uniques.py --- day18/Python/count_uniques.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/day18/Python/count_uniques.py b/day18/Python/count_uniques.py index 67d96cfe..bcb03960 100644 --- a/day18/Python/count_uniques.py +++ b/day18/Python/count_uniques.py @@ -11,3 +11,5 @@ def main(): lst = handle_input('an') frequency_dictionary = count_frequencies(lst) count_uniques(frequency_dictionary) + +main() From a97173b8b03602749da6e74ef6b6132b8a1ba795 Mon Sep 17 00:00:00 2001 From: Aadit Rahul Kamat Date: Tue, 15 Jan 2019 23:59:35 +0800 Subject: [PATCH 65/72] Add Python implementation for Day 19 and update Day 18 solutions Signed-off-by: Halid Altuner --- day18/Python/check_power_n.py | 2 -- day18/Python/count_uniques.py | 2 -- day18/Python/frequency_counter.py | 17 +++++---- day18/README.md | 16 +++++---- day19/Python/cartesian_product.py | 26 ++++++++++++++ day19/Python/fisher_yates.py | 21 ++++++++++++ day19/README.md | 57 +++++++++++++++++++++++++++++++ 7 files changed, 125 insertions(+), 16 deletions(-) create mode 100644 day19/Python/cartesian_product.py create mode 100644 day19/Python/fisher_yates.py diff --git a/day18/Python/check_power_n.py b/day18/Python/check_power_n.py index 6a32f74f..8b95af3d 100644 --- a/day18/Python/check_power_n.py +++ b/day18/Python/check_power_n.py @@ -22,5 +22,3 @@ def main(): print('Enter a number: ', end='') N = int(input()) check_power_n(first, second, N) - - main() diff --git a/day18/Python/count_uniques.py b/day18/Python/count_uniques.py index bcb03960..67d96cfe 100644 --- a/day18/Python/count_uniques.py +++ b/day18/Python/count_uniques.py @@ -11,5 +11,3 @@ def main(): lst = handle_input('an') frequency_dictionary = count_frequencies(lst) count_uniques(frequency_dictionary) - -main() diff --git a/day18/Python/frequency_counter.py b/day18/Python/frequency_counter.py index 62c95c7f..b94c78c5 100644 --- a/day18/Python/frequency_counter.py +++ b/day18/Python/frequency_counter.py @@ -14,13 +14,18 @@ def count_frequencies(lst): for key in frequency_dictionary: print(f'\'{key}\' is present {frequency_dictionary[key]} time(s)') +def get_input(word): + print(f'Enter {word} input array: ', end='') + input_array = input() + return input_array + +def handle_input(word): + input_array = get_input(word) + return convert_input_array_to_list(input_array) + def convert_input_array_to_list(input_array): return list(map(lambda x: int(x), input_array.strip('[').strip(']').split(','))) def main(): - print('Enter an input array: ', end='') - input_array = input() - lst = convert_input_array_to_list(input_array) - count_frequencies(lst) - -main() + lst = handle_input('an') + count_frequencies(lst) \ No newline at end of file diff --git a/day18/README.md b/day18/README.md index 365d6ad5..6542dbfa 100644 --- a/day18/README.md +++ b/day18/README.md @@ -219,7 +219,6 @@ for v,c in count.items(): ### [Solution 2](./Python/frequency_counter.py) ```python - """ author: @aaditkamat date: 15/01/2019 @@ -236,16 +235,21 @@ def count_frequencies(lst): for key in frequency_dictionary: print(f'\'{key}\' is present {frequency_dictionary[key]} time(s)') +def get_input(word): + print(f'Enter {word} input array: ', end='') + input_array = input() + return input_array + +def handle_input(word): + input_array = get_input(word) + return convert_input_array_to_list(input_array) + def convert_input_array_to_list(input_array): return list(map(lambda x: int(x), input_array.strip('[').strip(']').split(','))) def main(): - print('Enter an input array: ', end='') - input_array = input() - lst = convert_input_array_to_list(input_array) + lst = handle_input('an') count_frequencies(lst) - -main() ``` *** diff --git a/day19/Python/cartesian_product.py b/day19/Python/cartesian_product.py new file mode 100644 index 00000000..f2a7ba92 --- /dev/null +++ b/day19/Python/cartesian_product.py @@ -0,0 +1,26 @@ +""" +author: @aaditkamat +date: 15/01/2019 +""" +import sys +sys.path.append('../../day18/Python') + +from frequency_counter import convert_input_array_to_list + +def get_input(word): + print (f'Enter set {word}: ', end='') + input_array = input() + lst = convert_input_array_to_list(input_array) + return set(lst) + +def calculate_cartesian_product(first_set, second_set): + result = [] + for num in first_set: + for other in second_set: + result.append([num, other]) + print(result) + +def main(): + set_A = get_input('A') + set_B = get_input('B') + calculate_cartesian_product(set_A, set_B) \ No newline at end of file diff --git a/day19/Python/fisher_yates.py b/day19/Python/fisher_yates.py new file mode 100644 index 00000000..117666e8 --- /dev/null +++ b/day19/Python/fisher_yates.py @@ -0,0 +1,21 @@ +""" +author: @aaditkamat +date: 15/01/2019 +""" +import sys +sys.path.append('../../day18/Python') + +from frequency_counter import convert_input_array_to_list, handle_input +from random import randint + +def fisher_yates(lst): + for i in range(len(lst) - 1, 0, -1): + swap_index = randint(0, i) + temp = lst[swap_index] + lst[swap_index] = lst[i] + lst[i] = temp + print(lst) + +def main(): + lst = handle_input('an') + fisher_yates(lst) \ No newline at end of file diff --git a/day19/README.md b/day19/README.md index a61e29e5..0b1f3220 100644 --- a/day19/README.md +++ b/day19/README.md @@ -94,6 +94,37 @@ console.log (cartesian ([1, 2], [])); console.log (cartesian ([1, 2, 3, 4], ['a', 'b', 'c'])); ``` +### Python Implementation + +### [Solution](./Python/cartesian_product.py) +```python +''' +@author: aaditkamat +@date: 15/1/2019 +''' +import sys +sys.path.append('../../day18/Python') + +from frequency_counter import convert_input_array_to_list + +def get_input(word): + print (f'Enter set {word}: ', end='') + input_array = input() + lst = convert_input_array_to_list(input_array) + return set(lst) + +def calculate_cartesian_product(first_set, second_set): + result = [] + for num in first_set: + for other in second_set: + result.append([num, other]) + print(result) + +def main(): + set_A = get_input('A') + set_B = get_input('B') + calculate_cartesian_product(set_A, set_B) +``` *** ## Ques B @@ -139,4 +170,30 @@ function fisherYates (arr) { } fisherYates ([1, 2, 3, 4, 5, 6]); +``` + +### Python implementation + +#### [Solution](./Python/fisher_yates.py) + +```python +import sys +sys.path.append('../../day18/Python') + +from frequency_counter import convert_input_array_to_list, handle_input +from random import randint + +def fisher_yates(lst): + for i in range(len(lst) - 1, 0, -1): + swap_index = randint(0, i) + temp = lst[swap_index] + lst[swap_index] = lst[i] + lst[i] = temp + print(lst) + +def main(): + lst = handle_input('an') + fisher_yates(lst) + +main() ``` \ No newline at end of file From 8af374c5a489e4d56fb3fd851f3f85284bd3b614 Mon Sep 17 00:00:00 2001 From: Aadit Date: Wed, 16 Jan 2019 22:53:23 +0800 Subject: [PATCH 66/72] Add Ruby solution for Day 20 problem --- day20/Ruby/array_partition.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 day20/Ruby/array_partition.rb diff --git a/day20/Ruby/array_partition.rb b/day20/Ruby/array_partition.rb new file mode 100644 index 00000000..7b841230 --- /dev/null +++ b/day20/Ruby/array_partition.rb @@ -0,0 +1,30 @@ +=begin +@author: aaditkamat +@date: 16/01/2019 +=end +def partition(arr, n) + result = [] + ctr = 0 + until ctr == arr.size do + temp = [] + n.times do |i| + temp += [arr[ctr]] + ctr += 1 + if ctr == arr.size + break + end + end + result += [temp] + end + puts "The partitioned array is: #{result}" +end + +def main + print "Enter an array: " + arr = gets.chomp!.gsub(/\[|\]/, '').split(',').map!{|x| x.to_i} + print "Enter number of partitions: " + n = gets.chomp!.to_i + partition(arr, n) +end + +main From 2d7c8d2818934505fe30c95fb3cbe8dbaeb07535 Mon Sep 17 00:00:00 2001 From: Aadit Date: Wed, 16 Jan 2019 22:53:46 +0800 Subject: [PATCH 67/72] Update Day 20 README with Ruby solution --- day20/README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/day20/README.md b/day20/README.md index 10dac42f..5d1b82c4 100644 --- a/day20/README.md +++ b/day20/README.md @@ -86,3 +86,40 @@ function partition (array, size) { return partitionedArray; } ``` + +## Ruby Solution(s) + +### [Solution](./Ruby/array_partition.rb) + +```ruby +=begin +@author: aaditkamat +@date: 16/01/2019 +=end +def partition(arr, n) + result = [] + ctr = 0 + until ctr == arr.size do + temp = [] + n.times do |i| + temp += [arr[ctr]] + ctr += 1 + if ctr == arr.size + break + end + end + result += [temp] + end + puts "The partitioned array is: #{result}" +end + +def main + print "Enter an array: " + arr = gets.chomp!.gsub(/\[|\]/, '').split(',').map!{|x| x.to_i} + print "Enter number of partitions: " + n = gets.chomp!.to_i + partition(arr, n) +end + +main +``` \ No newline at end of file From 0d6908ae2abb1dd4b9d2b4a75e6aa7294d917663 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Mon, 28 Jan 2019 20:49:30 +0800 Subject: [PATCH 68/72] Add C++ solution and test cases for day 28 problem --- day28/C++/Aadit/sample001.ans | 1 + day28/C++/Aadit/sample001.in | 2 ++ day28/C++/Aadit/sample002.ans | 1 + day28/C++/Aadit/sample002.in | 2 ++ day28/C++/Aadit/solution.cpp | 21 +++++++++++++++++++++ day28/C++/Aadit/test.sh | 18 ++++++++++++++++++ 6 files changed, 45 insertions(+) create mode 100644 day28/C++/Aadit/sample001.ans create mode 100644 day28/C++/Aadit/sample001.in create mode 100644 day28/C++/Aadit/sample002.ans create mode 100644 day28/C++/Aadit/sample002.in create mode 100644 day28/C++/Aadit/solution.cpp create mode 100755 day28/C++/Aadit/test.sh diff --git a/day28/C++/Aadit/sample001.ans b/day28/C++/Aadit/sample001.ans new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/day28/C++/Aadit/sample001.ans @@ -0,0 +1 @@ +1 diff --git a/day28/C++/Aadit/sample001.in b/day28/C++/Aadit/sample001.in new file mode 100644 index 00000000..9d2b8984 --- /dev/null +++ b/day28/C++/Aadit/sample001.in @@ -0,0 +1,2 @@ +2 +1 2 3 4 5 diff --git a/day28/C++/Aadit/sample002.ans b/day28/C++/Aadit/sample002.ans new file mode 100644 index 00000000..417b7b53 --- /dev/null +++ b/day28/C++/Aadit/sample002.ans @@ -0,0 +1 @@ +undefined diff --git a/day28/C++/Aadit/sample002.in b/day28/C++/Aadit/sample002.in new file mode 100644 index 00000000..eaeda59b --- /dev/null +++ b/day28/C++/Aadit/sample002.in @@ -0,0 +1,2 @@ +7 +1 2 3 4 5 diff --git a/day28/C++/Aadit/solution.cpp b/day28/C++/Aadit/solution.cpp new file mode 100644 index 00000000..c4db8412 --- /dev/null +++ b/day28/C++/Aadit/solution.cpp @@ -0,0 +1,21 @@ +#include + +int main() { + int num; + std::cin >> num; + int ctr = 0; + bool flag = false; + while (!std::cin.eof()) { + int value; + std::cin >> value; + if (value == num) { + std::cout << ctr << std::endl; + flag = true; + } + ctr++; + } + if (!flag) { + std::cout << "undefined" << std::endl; + } + return 0; +} diff --git a/day28/C++/Aadit/test.sh b/day28/C++/Aadit/test.sh new file mode 100755 index 00000000..a6dc723d --- /dev/null +++ b/day28/C++/Aadit/test.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +clear +g++ solution.cpp -std=gnu++17 + +if [ $? -eq 0 ]; then + DIR=rationalsequence2 + + for i in {1..2} + do + ./a.out < ${DIR}/sample00${i}.in > ${DIR}/sample00${i}.o && diff ${DIR}/sample00${i}.o ${DIR}/sample00${i}.ans + open ${DIR}/sample00${i}.o + done +fi + + + + From 5ab5a045c453330e6e8c578517606a67dd87d8c0 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Mon, 28 Jan 2019 20:53:23 +0800 Subject: [PATCH 69/72] Add credits and brief explanation --- day28/C++/Aadit/solution.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/day28/C++/Aadit/solution.cpp b/day28/C++/Aadit/solution.cpp index c4db8412..88e0b39f 100644 --- a/day28/C++/Aadit/solution.cpp +++ b/day28/C++/Aadit/solution.cpp @@ -1,5 +1,15 @@ #include +/** + * @author: aaditkamat + * @date: 28/1/2019 + */ + +/* + * The input is assumed to be of the following form: + * The first line contains the number to be searched + * The second line contains the numbers to be searched seperated by spaces. + */ int main() { int num; std::cin >> num; From 7c610ead4dc57ddd6979e4f11b0709868eb7581f Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Mon, 28 Jan 2019 20:56:07 +0800 Subject: [PATCH 70/72] Add C++ solution to day 28 README --- day28/README.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/day28/README.md b/day28/README.md index 0402a276..eea70b62 100644 --- a/day28/README.md +++ b/day28/README.md @@ -37,4 +37,41 @@ function linearSearch (arr, n) { console.log (linearSearch ([1, 2, 3, 4, 5], 2)); console.log (linearSearch ([1, 2, 3, 4, 5], 7)); -``` \ No newline at end of file +``` + +### [C++ Implementation](./C++/Aadit/solution.cpp) + +```c++ +#include + +/** +* @author: aaditkamat +* @date: 28/1/2019 +*/ + +/* +* The input is assumed to be of the following form: +* The first line contains the number to be searched +* The second line contains the numbers to be searched seperated by spaces. +*/ +int main() { + int num; + std::cin >> num; + int ctr = 0; + bool flag = false; + while (!std::cin.eof()) { + int value; + std::cin >> value; + if (value == num) { + std::cout << ctr << std::endl; + flag = true; + } + ctr++; + } + + if (!flag) { + std::cout << "undefined" << std::endl; + } + return 0; +} +``` From 9f234aaa9e6f3a0bd03bc255c7549a2cec3250ae Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Tue, 12 Feb 2019 17:58:54 +0800 Subject: [PATCH 71/72] Add C++ solution for day 36 problem --- day36/C++/radix_sort.cpp | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 day36/C++/radix_sort.cpp diff --git a/day36/C++/radix_sort.cpp b/day36/C++/radix_sort.cpp new file mode 100644 index 00000000..855e8add --- /dev/null +++ b/day36/C++/radix_sort.cpp @@ -0,0 +1,69 @@ +/* + * @author: aaditkamat + * @date: 12/02/2019 + */ +#include +#include +#include +#include +#include +#define SIZE 10 + +/** + * The radix_sort works by processing decimal digits from right to left + * and sorting the numbers into buckets before combining them into a single + * array (mutating the original input array) at each step. The number of steps + * is equal to the number of decimal digits in the maximum element of the + * input array. + * + * e.g. input = [546, 37, 2132, 422] max_element = 2132, steps = digits(2132) = 4 + * Step 1: [[2132, 422], [546], [37]] input = [2132, 422, 546, 37] + * Step 2: [[422], [2132, 37], [546]] input = [422, 2132, 37, 546] + * Step 3: [[37], [2132], [422], [546]] input = [37, 2132, 422, 546] + * Step 4: [[37, 422, 546], [2132]] input = [37, 422, 546, 2132] + * + * The idea is that the smaller numbers (with lesser digits) will have 0s for the + * digits in the leftmost positions. Example can be written as 37 can be written + * as 0037 (0 * 10^3 + 0 * 10^2 + 3 * 10^1 + 7 * 10^0). If all numbers have them + * same number of digits then it will just sort according to 1st digit, breaking + * ties using the other digits. + */ +void printArray(std::vector output) { + for (int i = 0; i < output.size(); i++) { + std::cout << output[i] << " "; + } + std::cout << "\n"; +} + +int digits(int num) { + int ctr = 0; + while(num > 0) { + num /= 10; + ctr++; + } + return ctr; +} + +std::vector sort(std::vector input) { + int max = *max_element(input.begin(), input.end()); + for (int j = 0; j < digits(max); j++) { + std::array, SIZE> buckets; + for (int i = 0; i < input.size(); i++) { + int jth_digit = input[i] % static_cast(std::pow(10, j + 1)) / static_cast(std::pow(10, j)); + buckets[jth_digit].push_back(input[i]); + } + input = std::vector(); + for (int i = 0; i < SIZE; i++) { + for (int k = 0; k < buckets[i].size(); k++) { + input.push_back(buckets[i][k]); + } + } + } + return input; +} + +int main() { + std::vector input{39, 72, 64, 538, 2334, 39524, 1521, 2106, 3765}; + std::vector output = sort(input); + printArray(output); +} From 9065a1857ce995af34978ef9206fd1476306aaa0 Mon Sep 17 00:00:00 2001 From: Aadit Kamat Date: Tue, 12 Feb 2019 17:59:20 +0800 Subject: [PATCH 72/72] Add documentation for day 36 problem C++ implementation --- day36/README.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/day36/README.md b/day36/README.md index 27069dbb..728fc9cf 100644 --- a/day36/README.md +++ b/day36/README.md @@ -21,4 +21,78 @@ output: [1, 2, 3, 4, 5, 6, 7, 8, 9] ```js to be added -``` \ No newline at end of file +``` + +### [C++ Implementation](./C++/radix_sort.cpp) + +```c++ +/* + * @author: aaditkamat + * @date: 12/02/2019 + */ +#include +#include +#include +#include +#include +#define SIZE 10 + +/** + * The radix_sort works by processing decimal digits from right to left + * and sorting the numbers into buckets before combining them into a single + * array (mutating the original input array) at each step. The number of steps + * is equal to the number of decimal digits in the maximum element of the + * input array. + * + * e.g. input = [546, 37, 2132, 422] max_element = 2132, steps = digits(2132) = 4 + * Step 1: [[2132, 422], [546], [37]] input = [2132, 422, 546, 37] + * Step 2: [[422], [2132, 37], [546]] input = [422, 2132, 37, 546] + * Step 3: [[37], [2132], [422], [546]] input = [37, 2132, 422, 546] + * Step 4: [[37, 422, 546], [2132]] input = [37, 422, 546, 2132] + * + * The idea is that the smaller numbers (with lesser digits) will have 0s for the + * digits in the leftmost positions. Example can be written as 37 can be written + * as 0037 (0 * 10^3 + 0 * 10^2 + 3 * 10^1 + 7 * 10^0). If all numbers have them + * same number of digits then it will just sort according to 1st digit, breaking + * ties using the other digits. + */ +void printArray(std::vector output) { + for (int i = 0; i < output.size(); i++) { + std::cout << output[i] << " "; + } + std::cout << "\n"; +} + +int digits(int num) { + int ctr = 0; + while(num > 0) { + num /= 10; + ctr++; + } + return ctr; +} + +std::vector sort(std::vector input) { + int max = *max_element(input.begin(), input.end()); + for (int j = 0; j < digits(max); j++) { + std::array, SIZE> buckets; + for (int i = 0; i < input.size(); i++) { + int jth_digit = input[i] % static_cast(std::pow(10, j + 1)) / static_cast(std::pow(10, j)); + buckets[jth_digit].push_back(input[i]); + } + input = std::vector(); + for (int i = 0; i < SIZE; i++) { + for (int k = 0; k < buckets[i].size(); k++) { + input.push_back(buckets[i][k]); + } + } + } + return input; +} + +int main() { + std::vector input{39, 72, 64, 538, 2334, 39524, 1521, 2106, 3765}; + std::vector output = sort(input); + printArray(output); +} +```