Skip to content

Commit

Permalink
Day 3 - C & Python (#38)
Browse files Browse the repository at this point in the history
* Create HammingDistance.py

* Update README.md

* Create HammingDistance.c

* Update README.md

* Update HammingDistance.c

* Update HammingDistance.py
  • Loading branch information
ashwek authored and MadhavBahl committed Dec 24, 2018
1 parent 4181774 commit b113795
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
40 changes: 40 additions & 0 deletions Day3/C/HammingDistance.c
@@ -0,0 +1,40 @@
/**
* @author : ashwek
* @date : 24/12/2018
*/

#include<stdio.h>
#include<string.h>

int HammingDistance(char Str1[], char Str2[]){

int i, count = 0;

if( strlen(Str1) != strlen(Str2) )
return -1;

for(i=0; i<strlen(Str1); i++){
if( Str1[i] != Str2[i])
count++;
}

return count;
}

void main(){

char Str1[50], Str2[50];
int HD;

printf("Enter string 1 = ");
scanf("%s", Str1);
printf("Enter string 2 = ");
scanf("%s", Str2);

HD = HammingDistance(Str1, Str2);

if( HD == -1 )
printf("Strings are of different length");
else
printf("Hamming Distance = %d", HD);
}
26 changes: 26 additions & 0 deletions Day3/Python/HammingDistance.py
@@ -0,0 +1,26 @@
"""
@author : ashwek
@date : 24/12/2018
"""
def HammingDistance(Str1, Str2):

count = 0

if( len(Str1) != len(Str2) ):
return None

for i in range(len(Str1)):
if( Str1[i] != Str2[i]):
count += 1

return count

Str1 = input("Enter string 1 = ")
Str2 = input("Enter string 2 = ")

HD = HammingDistance(Str1, Str2)

if( HD is None ):
print("Strings are of different length")
else:
print("Hamming Distance = ", HD)
72 changes: 71 additions & 1 deletion Day3/README.md
Expand Up @@ -175,7 +175,77 @@ int main()
return 0;
}
```

### [Solution](./C/HammingDistance.c)

```c
#include<stdio.h>
#include<string.h>

int HammingDistance(char Str1[], char Str2[]){

int i, count = 0;

if( strlen(Str1) != strlen(Str2) )
return -1;

for(i=0; i<strlen(Str1); i++){
if( Str1[i] != Str2[i])
count++;
}

return count;
}

void main(){

char Str1[50], Str2[50];
int HD;

printf("Enter string 1 = ");
scanf("%s", Str1);
printf("Enter string 2 = ");
scanf("%s", Str2);

HD = HammingDistance(Str1, Str2);

if( HD == -1 )
printf("Strings are of different length");
else
printf("Hamming Distance = %d", HD);
}
```
## Python Implementation
### [Solution](./Python/HammingDistance.py)
```python
def HammingDistance(Str1, Str2):
count = 0
if( len(Str1) != len(Str2) ):
return None
for i in range(len(Str1)):
if( Str1[i] != Str2[i]):
count += 1
return count
Str1 = input("Enter string 1 = ")
Str2 = input("Enter string 2 = ")
HD = HammingDistance(Str1, Str2)
if( HD is None ):
print("Strings are of different length")
else:
print("Hamming Distance = ", HD)
```

## 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.
So, to keep everyone (who is following the DailyCodes challenge) consistent, I have decided to keep today's question very simple and straightforward.

0 comments on commit b113795

Please sign in to comment.