Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ab7436a

Browse files
Add DNA_Matching problem to (L-I).
1 parent a9672bd commit ab7436a

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

‎L-I/0011 DNA_Matching/DNA_Matching.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function dnaMatch(dna1, dna2) {
2+
// Check if the lengths of the two DNA sequences are different
3+
if (dna1.length !== dna2.length) {
4+
return false; // Different lengths, not a match
5+
}
6+
7+
// Iterate through each character of the DNA sequences
8+
for (let i = 0; i < dna1.length; i++) {
9+
// Compare characters at the current position
10+
if (dna1[i] !== dna2[i]) {
11+
return false; // Characters don't match, not a match
12+
}
13+
}
14+
15+
// If we reach this point, all characters match, so it's a match
16+
return true;
17+
}
18+
19+
// Test cases
20+
console.log(dnaMatch("ACGT", "ACGT")); // true
21+
console.log(dnaMatch("ACGT", "ACTG")); // false
22+
console.log(dnaMatch("AACT", "AACC")); // false

‎L-I/0011 DNA_Matching/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 0011 DNA_Matching ( L-I )
2+
3+
## Problem
4+
5+
You are given two DNA sequences as strings, representing the genetic code of two individuals. Each DNA sequence consists of a series of characters, where each character can be one of four options: 'A' (adenine), 'C' (cytosine), 'G' (guanine), or 'T' (thymine).
6+
7+
Write a JavaScript function called dnaMatch that takes these two DNA sequences as input and returns true if they are a match and false otherwise. Two DNA sequences are considered a match if they have the same characters in the same order.
8+
9+
**Example**
10+
11+
```
12+
If the input DNA sequences are "ACGT" and "ACGT", the function should return true because they are an exact match.
13+
If the input DNA sequences are "ACGT" and "ACTG", the function should return false because the order of characters is different.
14+
If the input DNA sequences are "AACT" and "AACC", the function should return true because some characters do not match.
15+
```
16+
17+
**Here's an example of how the function should work:**
18+
19+
```javascript
20+
console.log(dnaMatch("ACGT", "ACGT")); // true
21+
console.log(dnaMatch("ACGT", "ACTG")); // false
22+
console.log(dnaMatch("AACT", "AACC")); // false
23+
```
24+
25+
## Solutions
26+
27+
```javascript
28+
function dnaMatch(dna1, dna2) {
29+
// Check if the lengths of the two DNA sequences are different
30+
if (dna1.length !== dna2.length) {
31+
return false; // Different lengths, not a match
32+
}
33+
34+
// Iterate through each character of the DNA sequences
35+
for (let i = 0; i < dna1.length; i++) {
36+
// Compare characters at the current position
37+
if (dna1[i] !== dna2[i]) {
38+
return false; // Characters don't match, not a match
39+
}
40+
}
41+
42+
// If we reach this point, all characters match, so it's a match
43+
return true;
44+
}
45+
46+
// Test cases
47+
console.log(dnaMatch("ACGT", "ACGT")); // true
48+
console.log(dnaMatch("ACGT", "ACTG")); // false
49+
console.log(dnaMatch("AACT", "AACC")); // false
50+
```
51+
52+
## How it works
53+
54+
1. The `dnaMatch` function takes two input arguments: `dna1` and `dna2`, which are the two DNA sequences we want to compare.
55+
56+
2. First, it checks if the lengths of `dna1` and `dna2` are different using the `if (dna1.length !== dna2.length)` conditional statement. If they have different lengths, the function immediately returns `false` because sequences of different lengths cannot be a match.
57+
58+
3. If the lengths are the same, the function proceeds to compare the characters of the two DNA sequences.
59+
60+
4. It enters a `for` loop that iterates through the characters of the sequences. The loop is controlled by the variable `i`, which starts at 0 and goes up to `dna1.length - 1`. This loop allows us to compare characters at the same position in both sequences.
61+
62+
5. Inside the loop, the code compares the characters at the current position `i` using the conditional statement `if (dna1[i] !== dna2[i])`. If the characters at position `i` are not the same in both sequences, the function immediately returns `false` because it found a mismatch.
63+
64+
6. If the loop completes without finding any mismatches, meaning it has compared all characters in both sequences, the function returns `true`. This indicates that the two DNA sequences are identical and, therefore, a match.
65+
66+
## References
67+
68+
- [ChatGPT](https://chat.openai.com/)
69+
70+
## Problem Added By
71+
72+
- [Tipchan](https://github.com/tsongtheng)
73+
74+
## Contributing
75+
76+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
77+
78+
Please make sure to update tests as appropriate.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /