Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First round of converted samples #6

Merged
merged 52 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
7f61067
Adding to BigIntMod python code
vprusso Jun 28, 2019
d1ad539
Adding FindDuplicates python code
vprusso Jun 28, 2019
6732f06
Adding IntToRoman python
vprusso Jun 28, 2019
7ce9b4b
Rename to be consistent
vprusso Jun 28, 2019
2b1be53
Adding LinkedListCycle
vprusso Jun 28, 2019
23bf9de
Adding NthToLast Python code
vprusso Jun 28, 2019
5f97fbc
Added ZeroMatrix Python file
vprusso Jun 28, 2019
bc5daf7
Adding to ConsecutiveArray Python file
vprusso Jun 29, 2019
bc29339
Adding to BuildOrder Python file
vprusso Jul 1, 2019
3264df1
Adding to Python solutions
vprusso Jul 3, 2019
1b46d29
Update to README
vprusso Jul 3, 2019
20a791b
Adding to README
vprusso Jul 3, 2019
4ff2e40
Adding to README
vprusso Jul 3, 2019
439e265
Adding Knapsack to code
vprusso Jul 6, 2019
aa910e7
Updated README
vprusso Jul 6, 2019
b40e81d
Added to README
vprusso Jul 6, 2019
d3e8213
Updating README file
vprusso Jul 6, 2019
75803d6
Updating README
vprusso Jul 6, 2019
90d3380
Adding to README
vprusso Jul 6, 2019
e430ca1
Adding CPP FizzBuzz
vprusso Jul 6, 2019
80237f4
Updates to readme
vprusso Jul 6, 2019
ce3d44d
Adding Java FizzBuzz
vprusso Jul 7, 2019
645eb31
Adding Ones Binary in Python and Java
vprusso Jul 7, 2019
1fc57cf
Adding to README
vprusso Jul 7, 2019
31a700e
Adding to Anagrams and OnesInBinary
vprusso Jul 9, 2019
40cb86b
Updates to README
vprusso Jul 9, 2019
f87536f
Adding to String Deletion
vprusso Jul 9, 2019
ec20fd4
Adding Sum problem for Python and Java
vprusso Jul 11, 2019
cd02973
Adding to Fibonacci for Python and Java
vprusso Jul 11, 2019
f97743c
Adding CPP for Fibonacci
vprusso Jul 11, 2019
8422507
Adding to smallest change for Java and Python
vprusso Jul 11, 2019
9cb7b5d
t
vprusso Jul 11, 2019
505ff75
Adding to Swap variables for all CPP,Python, and Java
vprusso Jul 11, 2019
4a329e8
Adding to ClockAngle code
vprusso Jul 11, 2019
160cbb1
IntToRoman C++
vprusso Jul 18, 2019
367653f
Adding to dedup linked list
vprusso Jul 19, 2019
5beac7b
Adding to dedup linked list
vprusso Jul 21, 2019
bda4e27
Adding to Random linked list
vprusso Jul 22, 2019
262f3a9
Adding to FindDuplicates for C++
vprusso Jul 25, 2019
f03af31
Adding to Two Missing
vprusso Jul 25, 2019
bccd4f4
Adding to ReverseStack for Java and Python
vprusso Jul 31, 2019
0ec613f
Adding to ReverseStack
vprusso Aug 1, 2019
4ed5c47
Adding to Gray Code
vprusso Aug 1, 2019
8b2a18b
Adding to PrintReversedLsit
vprusso Aug 1, 2019
70e366a
Adding to README
vprusso Aug 1, 2019
d6d50e6
Adding to RotateBits
vprusso Aug 2, 2019
ce8e85b
Adding to string compression for Java and Python
vprusso Aug 3, 2019
a62f894
Adding README
vprusso Aug 3, 2019
13775cd
Updates to StringCompression python file
vprusso Aug 4, 2019
5441eb0
Adding to line intersection
vprusso Aug 4, 2019
ccb3f6b
Adding to MaxStack
vprusso Aug 12, 2019
86d8111
Adding to TreeLevelOrder
vprusso Aug 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding to Anagrams and OnesInBinary
  • Loading branch information
vprusso committed Jul 9, 2019
commit 31a700ecae4dbd3dc839a9df8e5a8db7d5f3deb7
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ Given a list of strings, write a function to get the kth most frequently occurri
Given a string, write a function to compress it by shortening every sequence of the same character to that character followed by the number of repetitions. If the compressed string is longer than the original, you should return the original string.

* [Anagrams](http://www.byte-by-byte.com/anagrams/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/Anagrams.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/Anagrams.py))
Given two strings, write a function to determine whether they are anagrams.

* [Binary Search Tree Verification](http://www.byte-by-byte.com/binarysearchtree/)
Expand All @@ -177,6 +179,7 @@ Given a binary tree, write a function to test if the tree is a binary search tre
Implement a LIFO stack that has a `push()`, `pop()`, and `max()` function, where `max()` returns the maximum value in the stack. All of these functions should run in `O(1)` time.

* [Number of Ones in Binary](http://www.byte-by-byte.com/onesinbinary/)
([C++](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/cpp/OnesInBinary.cpp))
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/OnesInBinary.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/OnesInBinary.py))<br>
Given an integer, write a function to compute the number of ones in the binary representation of the number.
Expand Down
38 changes: 38 additions & 0 deletions cpp/OnesInBinary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Title: Number of ones in binary representation.
*
* Given an integer, write a function to compute the number of ones in
* the binary representation of the number.
*
* Execution: g++ OnesInBinary.cpp -o OnesInBinary
*
* For more details, check out http://www.byte-by-byte.com/onesinbinary
*/
#include <iostream>

using namespace std;

int onesInBinary(int x) {
int sum = 0;
while (x > 0) {
sum += x & 1;
x >>= 1;
}
return sum;
}

int main() {
assert(onesInBinary(5) == 2);
cout << "Binary representation of 5 has 2 ones" << endl;

assert(onesInBinary(12) == 2);
cout << "Binary representation of 12 has 2 ones" << endl;

assert(onesInBinary(255) == 8);
cout << "Binary representation of 255 has 8 ones" << endl;

assert(onesInBinary(256) == 1);
cout << "Binary representation of 256 has 1 ones" << endl;

return 0;
}
50 changes: 50 additions & 0 deletions java/Anagrams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Title: Anagrams
*
* Given two strings, write a function to determine whether they are anagrams.
*
*
* Execution: javac Anagrams.java && java Anagrams

* For more details, check out http://www.byte-by-byte.com/anagrams
*/

public class Anagrams {
public static boolean isAnagram(String s1, String s2) {
if (s1.length() != s2.length()) return false;

s1 = s1.toLowerCase();
s2 = s2.toLowerCase();

int[] letters = new int[1<<8];

for (char c : s1.toCharArray()) {
letters[c]++;
}

for (char c : s2.toCharArray()) {
letters[c]--;
}

for (int i : letters) {
if (i != 0) return false;
}
return true;
}

// Sample test cases
public static void main(String[] args) {
assert isAnagram("", "") == true:
"Empty string s1='' and string s2='' are anagrams";
assert isAnagram("A", "A") == true:
"Empty string s1='A' and string s2='A' are anagrams";
assert isAnagram("A", "B") == false:
"Empty string s1='A' and string s2='B' are not anagrams";
assert isAnagram("ab", "ba") == true:
"Empty string s1='ab' and string s2='ba' are anagrams";
assert isAnagram("AB", "ab") == true:
"Empty string s1='AB' and string s2='ab' are anagrams";

System.out.println("Passed all test cases");
}
}
55 changes: 55 additions & 0 deletions python/Anagrams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Title: Is anagram

Given two strings, write a function to determine whether they are anagrams.

Execution: python Anagrams.py

For more details, check out http://www.byte-by-byte.com/anagrams/
"""
from collections import defaultdict
import unittest


def is_anagram(s1: str, s2: str):
if len(s1) != len(s2):
return False

s1 = s1.lower()
s2 = s2.lower()

letters = defaultdict(int)

for c in s1:
letters[c] += 1

for c in s2:
letters[c] -= 1

for i in letters.values():
if i != 0:
return False
return True


class TestAnagram(unittest.TestCase):
def test_1(self):
self.assertEqual(is_anagram("", ""), True)
print("Empty string s1='' and string s2='' are anagrams")

def test_2(self):
self.assertEqual(is_anagram("A", "B"), False)
print("Empty string s1='A' and string s2='B' are not anagrams")

def test_3(self):
self.assertEqual(is_anagram("ab", "ba"), True)
print("Empty string s1='ab' and string s2='ba' are anagrams")

def test_4(self):
self.assertEqual(is_anagram("AB", "ab"), True)
print("Empty string s1='AB' and string s2='ab' are anagrams")


if __name__ == '__main__':
unittest.main()