How to update a value with the result from a function - replace

let rec insert v i l =
match i, l with
| 0, xs -> v::xs
| i, x::xs -> x::insert v (i - 1) xs
| i, [] -> failwith "index out of range"
let rec remove i l =
match i, l with
| 0, x::xs -> xs
| i, x::xs -> x::remove (i - 1) xs
| i, [] -> failwith "index out of range"
The su value looks as follows:
[['5'; '3'; '*'; '*'; '7'; '*'; '*'; '*'; '*'];
['6'; '*'; '*'; '1'; '9'; '5'; '*'; '*'; '*'];
['*'; '9'; '8'; '*'; '*'; '*'; '*'; '6'; '*'];
['8'; '*'; '*'; '*'; '6'; '*'; '*'; '*'; '3'];
['4'; '*'; '*'; '8'; '*'; '3'; '*'; '*'; '1'];
['7'; '*'; '*'; '*'; '2'; '*'; '*'; '*'; '6'];
['*'; '6'; '*'; '*'; '*'; '*'; '2'; '8'; '*'];
['*'; '*'; '*'; '4'; '1'; '9'; '*'; '*'; '5'];
['*'; '*'; '*'; '*'; '8'; '*'; '*'; '7'; '9']]
The goal is to replace one of the lists in su, with the new list generated from the replace function, and then make that result saved as the new su. This is what i have made so far.
let replace (r,s,v : int) =
remove (s-1) su.[r-1]
insert (char(string(v))) (s-1) su.[r-1]
with the call replace (2,2,4), su should end up as follows:
[['5'; '3'; '*'; '*'; '7'; '*'; '*'; '*'; '*'];
['6'; '4'; '*'; '1'; '9'; '5'; '*'; '*'; '*'];
['*'; '9'; '8'; '*'; '*'; '*'; '*'; '6'; '*'];
['8'; '*'; '*'; '*'; '6'; '*'; '*'; '*'; '3'];
['4'; '*'; '*'; '8'; '*'; '3'; '*'; '*'; '1'];
['7'; '*'; '*'; '*'; '2'; '*'; '*'; '*'; '6'];
['*'; '6'; '*'; '*'; '*'; '*'; '2'; '8'; '*'];
['*'; '*'; '*'; '4'; '1'; '9'; '*'; '*'; '5'];
['*'; '*'; '*'; '*'; '8'; '*'; '*'; '7'; '9']]

Unless you insists on doing it recursively the following may do the trick:
let su =
[['5'; '3'; '*'; '*'; '7'; '*'; '*'; '*'; '*'];
['6'; '*'; '*'; '1'; '9'; '5'; '*'; '*'; '*'];
['*'; '9'; '8'; '*'; '*'; '*'; '*'; '6'; '*'];
['8'; '*'; '*'; '*'; '6'; '*'; '*'; '*'; '3'];
['4'; '*'; '*'; '8'; '*'; '3'; '*'; '*'; '1'];
['7'; '*'; '*'; '*'; '2'; '*'; '*'; '*'; '6'];
['*'; '6'; '*'; '*'; '*'; '*'; '2'; '8'; '*'];
['*'; '*'; '*'; '4'; '1'; '9'; '*'; '*'; '5'];
['*'; '*'; '*'; '*'; '8'; '*'; '*'; '7'; '9']]
let replaceV s v i c =
if i + 1 = s then v else c
let replaceL r s v i l =
let replaceV' = replaceV s v
match i with
| _ when r = i + 1 -> l |> List.mapi replaceV'
| _ -> l
// x y value
let replace r s v =
let replace' = replaceL r s v
su |> List.mapi replace'
replace 2 2 '4'
I'm not to happy with this code myself though...
If you insists on doing it recursively and using your code, you possible see by this example how/what you may do to your own.

To transform a list of lists, two cascaded List.map are needed. With List.mapi the index of the element is also given.
With the column and row index it is easy to do the replacement at the correct position.
let su =
[['5'; '3'; '*'; '*'; '7'; '*'; '*'; '*'; '*'];
['6'; '*'; '*'; '1'; '9'; '5'; '*'; '*'; '*'];
['*'; '9'; '8'; '*'; '*'; '*'; '*'; '6'; '*'];
['8'; '*'; '*'; '*'; '6'; '*'; '*'; '*'; '3'];
['4'; '*'; '*'; '8'; '*'; '3'; '*'; '*'; '1'];
['7'; '*'; '*'; '*'; '2'; '*'; '*'; '*'; '6'];
['*'; '6'; '*'; '*'; '*'; '*'; '2'; '8'; '*'];
['*'; '*'; '*'; '4'; '1'; '9'; '*'; '*'; '5'];
['*'; '*'; '*'; '*'; '8'; '*'; '*'; '7'; '9']]
let replace su col row r =
List.mapi (fun i xs ->
List.mapi (fun j x ->
if (i+1)=col && (j+1)=row
then r else x) xs) su
replace su 2 2 '4'

I have a solution for your problem. It consists of two functions. The first replaces a value v in a list at positions i. The second function calls the first one and replaces the list with the number i.
let rec replaceInList v i lst =
match i, lst with
| _, [] -> []
| 1, x::xs -> v::xs
| i, x::xs -> x::replaceInList v (i-1) xs
let rec replaceInMatrix v i j matrix =
match i, matrix with
| _, [] -> []
| 1, x::xs -> (replaceInList v j x)::xs
| i, x::xs -> x::replaceInMatrix v (i-1) j xs
You can test both in the F# REPL:
> replace 9 2 [1;2;3];;
val it : int list = [1; 9; 3]
> replaceInMatrix 99 2 2 [[1;2;3];[4;5;6];[7;8;9]];;
val it : int list list = [[1; 2; 3]; [4; 99; 6]; [7; 8; 9]]
The second function is basically a repetition of the first one. Maybe there is a way to create a more general implementation but unfortunately I'm not fluent enough in F# to come up with a better solution. Maybe someone else...

Related

Error in dec to hex converting function with little endian

I've written a function that does the conversion, but it has an error: 1000 as input gives E8 E3 as little endian, or E3E8 as hex, but it should be 03E8, or E8 03 in little endian format.
I literally don't have a clue what might be wrong.
I know the code might be pretty big for it's purpose, but I'd really like to know what's wrong with the error, all other numbers work fine, even negative ones
string decToHex(int num) {
string ret = "";
char c1='0', c2='0';
int res;
if(num>0){
while (num != 0) {
res = num % 16;
num = num / 16;
switch (res) {
case 0: c2 = '0'; break;
case 1: c2 = '1'; break;
case 2: c2 = '2'; break;
case 3: c2 = '3'; break;
case 4: c2 = '4'; break;
case 5: c2 = '5'; break;
case 6: c2 = '6'; break;
case 7: c2 = '7'; break;
case 8: c2 = '8'; break;
case 9: c2 = '9'; break;
case 10: c2 = 'A'; break;
case 11: c2 = 'B'; break;
case 12: c2 = 'C'; break;
case 13: c2 = 'D'; break;
case 14: c2 = 'E'; break;
case 15: c2 = 'F'; break;
}
if (num != 0) {
res = num % 16;
num = num / 16;
switch (res) {
case 0: c1 = '0'; break;
case 1: c1 = '1'; break;
case 2: c1 = '2'; break;
case 3: c1 = '3'; break;
case 4: c1 = '4'; break;
case 5: c1 = '5'; break;
case 6: c1 = '6'; break;
case 7: c1 = '7'; break;
case 8: c1 = '8'; break;
case 9: c1 = '9'; break;
case 10: c1 = 'A'; break;
case 11: c1 = 'B'; break;
case 12: c1 = 'C'; break;
case 13: c1 = 'D'; break;
case 14: c1 = 'E'; break;
case 15: c1 = 'F'; break;
}
}
ret += c1;
ret += c2; ret += " ";
}
return ret;
}
else if(num<0){
unsigned int n = num;
while (n != 0) {
res = n % 16;
n = n / 16;
switch (res) {
case 0: c2 = '0'; break;
case 1: c2 = '1'; break;
case 2: c2 = '2'; break;
case 3: c2 = '3'; break;
case 4: c2 = '4'; break;
case 5: c2 = '5'; break;
case 6: c2 = '6'; break;
case 7: c2 = '7'; break;
case 8: c2 = '8'; break;
case 9: c2 = '9'; break;
case 10: c2 = 'A'; break;
case 11: c2 = 'B'; break;
case 12: c2 = 'C'; break;
case 13: c2 = 'D'; break;
case 14: c2 = 'E'; break;
case 15: c2 = 'F'; break;
}
if (n != 0) {
res = n % 16;
n = n / 16;
switch (res) {
case 0: c1 = '0'; break;
case 1: c1 = '1'; break;
case 2: c1 = '2'; break;
case 3: c1 = '3'; break;
case 4: c1 = '4'; break;
case 5: c1 = '5'; break;
case 6: c1 = '6'; break;
case 7: c1 = '7'; break;
case 8: c1 = '8'; break;
case 9: c1 = '9'; break;
case 10: c1 = 'A'; break;
case 11: c1 = 'B'; break;
case 12: c1 = 'C'; break;
case 13: c1 = 'D'; break;
case 14: c1 = 'E'; break;
case 15: c1 = 'F'; break;
}
}
ret += c1;
ret += c2; ret += " ";
}
return ret;
}
else {
return "00 00 ";
}
}
I propose alternative solution, shorter.
#include <iostream>
#include <string>
template <typename I> std::string n2hexstr(I w, int hex_len = sizeof(I)<<1) {
static const char* const digits = "0123456789ABCDEF";
std::string rc(hex_len,'0');
auto it = rc.begin();
for (int i = 0; i < hex_len; i++) {
*it++ = digits[(w >> (i*8 + 4)) & 0x0F];
*it++ = digits[(w >> (i*8)) & 0x0F];
}
return rc;
}
int main()
{
std::cout << n2hexstr( static_cast<uint16_t>(1000) ) << std::endl;
return 0;
}
Was missing an else that needed to set the old c1 to '0' ascii, silly me :)
string decToHex(int num) {
string ret = "";
char c1='0', c2='0';
int res;
if(num>0){
while (num != 0) {
res = num % 16;
num = num / 16;
switch (res) {
case 0: c2 = '0'; break;
case 1: c2 = '1'; break;
case 2: c2 = '2'; break;
case 3: c2 = '3'; break;
case 4: c2 = '4'; break;
case 5: c2 = '5'; break;
case 6: c2 = '6'; break;
case 7: c2 = '7'; break;
case 8: c2 = '8'; break;
case 9: c2 = '9'; break;
case 10: c2 = 'A'; break;
case 11: c2 = 'B'; break;
case 12: c2 = 'C'; break;
case 13: c2 = 'D'; break;
case 14: c2 = 'E'; break;
case 15: c2 = 'F'; break;
}
if (num != 0) {
res = num % 16;
num = num / 16;
switch (res) {
case 0: c1 = '0'; break;
case 1: c1 = '1'; break;
case 2: c1 = '2'; break;
case 3: c1 = '3'; break;
case 4: c1 = '4'; break;
case 5: c1 = '5'; break;
case 6: c1 = '6'; break;
case 7: c1 = '7'; break;
case 8: c1 = '8'; break;
case 9: c1 = '9'; break;
case 10: c1 = 'A'; break;
case 11: c1 = 'B'; break;
case 12: c1 = 'C'; break;
case 13: c1 = 'D'; break;
case 14: c1 = 'E'; break;
case 15: c1 = 'F'; break;
}
}
else c1='0';
ret += c1;
ret += c2; ret += " ";
}
return ret;
}
else if(num<0){
unsigned int n = num;
while (n != 0) {
res = n % 16;
n = n / 16;
switch (res) {
case 0: c2 = '0'; break;
case 1: c2 = '1'; break;
case 2: c2 = '2'; break;
case 3: c2 = '3'; break;
case 4: c2 = '4'; break;
case 5: c2 = '5'; break;
case 6: c2 = '6'; break;
case 7: c2 = '7'; break;
case 8: c2 = '8'; break;
case 9: c2 = '9'; break;
case 10: c2 = 'A'; break;
case 11: c2 = 'B'; break;
case 12: c2 = 'C'; break;
case 13: c2 = 'D'; break;
case 14: c2 = 'E'; break;
case 15: c2 = 'F'; break;
}
if (n != 0) {
res = n % 16;
n = n / 16;
switch (res) {
case 0: c1 = '0'; break;
case 1: c1 = '1'; break;
case 2: c1 = '2'; break;
case 3: c1 = '3'; break;
case 4: c1 = '4'; break;
case 5: c1 = '5'; break;
case 6: c1 = '6'; break;
case 7: c1 = '7'; break;
case 8: c1 = '8'; break;
case 9: c1 = '9'; break;
case 10: c1 = 'A'; break;
case 11: c1 = 'B'; break;
case 12: c1 = 'C'; break;
case 13: c1 = 'D'; break;
case 14: c1 = 'E'; break;
case 15: c1 = 'F'; break;
}
}
else c1='0';
ret += c1;
ret += c2; ret += " ";
}
return ret;
}
else {
return "00 00 ";
}
}

How it could be optimized [C++]?

I was doing some C++ "homework" and I've made an exercise which says the following:
Input
The input consists of a sequence of characters, which include exclusively lowercase letters, spaces and newlines.
[Table with letter-number values]
Output
Your program must print the total value of the message, computed as the sum of the value of all its letters.
And I've done this!:
#include<iostream>
using namespace std;
int main() {
char v;
int val = 0;
while(cin >> v){
if (v == 'a' or v == 'e') val += 1;
if (v == 'o' or v == 's') val += 2;
if (v == 'd' or v == 'i' or v == 'n' or v == 'r') val += 3;
if (v == 'c' or v == 'l' or v == 't' or v == 'u') val += 4;
if (v == 'm' or v == 'p') val += 5;
if (v == 'k' or v == 'w') val += 7;
if (v == 'b' or v == 'f' or v == 'g' or v == 'h') val += 6;
if (v == 'j' or v == 'q' or v == 'v' or v == 'x' or v == 'y' or v == 'z') val += 6;
}
cout << val << endl;
}
Example:
INPUT:
is a chinese wok
OUTPUT:
42
(I end the While loop by pressing Ctrl+D, which is how the evaluation web does.)
Which is a pretty simple and working solution but...
I was wandering if there's any way of doing this without a bunch of "if's". Gotta say, I can't include anything else than iostream.
Thanks!
As another answer indicates, you can use a map. That's the most compact way of writing this algorithm, but it will not necessarily result in best performance.
There are two more ways that I can think of. A somewhat better way is with a switch statement, like this:
int get_increment( char v )
{
switch( v )
{
case 'a': case 'e':
return 1;
case 'o': case 's':
return 2;
case 'd': case 'i': case 'n': case 'r':
return 3;
case 'c': case 'l': case 't': case 'u':
return 4;
case 'm': case 'p':
return 5;
case 'k': case 'b': case 'f': case 'g': case 'h':
return 6;
case 'w':
return 7;
case 'j': case 'q': case 'v': case 'x': case 'y': case 'z':
return 6;
default:
return 0; //not a letter!
}
}
but if you want the maximum performance, then a lookup table is the way to go.
Here is how to initialize the lookup table:
int increments[256];
for( int i = 0; i < 256; i++ )
increments[i] = 0;
for( char c = 'a'; c <= 'z'; c++ )
increments[c] = get_increment( c );
and then here is how to use it:
val += increments[(unsigned char)v];
Note: the cast of v to unsigned char is not strictly speaking necessary, if v is only going to contain letters. But it will save your program from a crash if characters in your architecture are signed, (and they usually are,) and v happens to contain a negative value.

RNA to protein. Program compiles but won't stop running

I recently learned about multidimensional arrays and was given the task of analyzing strands of RNA and translating them into sequences of protein. I decided to use my knowledge of multidimensional arrays to create a definition of each amino acid a codon (group of 3 RNA bases) would translate to.
//RNA codon to amino acid mapping
char aminoAcid[4][4][4];
//A = 0, C = 1, G = 2, U = 3
//phenylalanine - F
aminoAcid[3][3][3] = 'F';
aminoAcid[3][3][1] = 'F';
//Leucine - L
aminoAcid[3][3][0] = 'L';
aminoAcid[3][3][2] = 'L';
//Serine - S
aminoAcid[3][1][3] = 'S';
aminoAcid[3][1][1] = 'S';
aminoAcid[3][1][0] = 'S';
aminoAcid[3][1][2] = 'S';
//tyrosine - Y
aminoAcid[3][0][3] = 'Y';
aminoAcid[3][0][1] = 'Y';
//stop codon
aminoAcid[3][0][0] = '-';
aminoAcid[3][0][2] = '-';
//cysteine - C
aminoAcid[3][2][3] = 'C';
aminoAcid[3][2][1] = 'C';
//stop codon
aminoAcid[3][2][0] = '-';
//tryptophan - W
aminoAcid[3][2][2] = 'W';
//leucine - L
aminoAcid[1][3][3] = 'L';
aminoAcid[1][3][1] = 'L';
aminoAcid[1][3][0] = 'L';
aminoAcid[1][3][2] = 'L';
//proline - P
aminoAcid[1][1][3] = 'P';
aminoAcid[1][1][1] = 'P';
aminoAcid[1][1][0] = 'P';
aminoAcid[1][1][2] = 'P';
//histidine - H
aminoAcid[1][0][3] = 'H';
aminoAcid[1][0][1] = 'H';
//glutamine - Q
aminoAcid[1][0][0] = 'Q';
aminoAcid[1][0][2] = 'Q';
//arginine - R
aminoAcid[1][2][3] = 'R';
aminoAcid[1][2][1] = 'R';
aminoAcid[1][2][0] = 'R';
aminoAcid[1][2][2] = 'R';
//isoleucine - I
aminoAcid[0][3][3] = 'I';
aminoAcid[0][3][1] = 'I';
aminoAcid[0][3][0] = 'I';
//methionine(start codon) - M
aminoAcid[0][3][2] = 'M';
//threonine -T
aminoAcid[0][1][3] = 'T';
aminoAcid[0][1][1] = 'T';
aminoAcid[0][1][0] = 'T';
aminoAcid[0][1][2] = 'T';
//asparagine - N
aminoAcid[0][0][3] = 'N';
aminoAcid[0][0][1] = 'N';
//lysine - K
aminoAcid[0][0][0] = 'K';
aminoAcid[0][0][2] - 'K';
//serine - S
aminoAcid[0][2][3] = 'S';
aminoAcid[0][2][1] = 'S';
//arginine - R
aminoAcid[0][2][0] = 'R';
aminoAcid[0][2][2] = 'R';
//valine - V
aminoAcid[2][3][3] = 'V';
aminoAcid[2][3][1] = 'V';
aminoAcid[2][3][0] = 'V';
aminoAcid[2][3][2] = 'V';
//alanine - A
aminoAcid[2][1][3] = 'A';
aminoAcid[2][1][1] = 'A';
aminoAcid[2][1][0] = 'A';
aminoAcid[2][1][2] = 'A';
//aspartic acid - D
aminoAcid[2][0][3] = 'D';
aminoAcid[2][0][1] = 'D';
//glutamic acid - E
aminoAcid[2][0][0] = 'E';
aminoAcid[2][0][2] = 'E';
//glycine - G
aminoAcid[2][2][3] = 'G';
aminoAcid[2][2][1] = 'G';
aminoAcid[2][2][0] = 'G';
aminoAcid[2][2][2] = 'G';
I created the following function to translate the strand. In this case, please note that my rna strand is:
AUGCUUAUUAACUGAAAACAUAUGGGUAGUCGAUGA
string rnaAnalysis::translateRna()
{
string protein = "";
int firstBase, secondBase, thirdBase;
for(int i = 0; i < newSequence.length() - 2; i+3)
{
if(newSequence[i] == 'A')
{
firstBase = 0;
}
else if(newSequence[i] == 'C')
{
firstBase = 1;
}
else if(newSequence[i] == 'G')
{
firstBase = 2;
}
else if(newSequence[i] == 'U')
{
firstBase = 3;
}
if(newSequence[i+1] == 'A')
{
secondBase = 0;
}
else if(newSequence[i+1] == 'C')
{
secondBase = 1;
}
else if(newSequence[i+1] == 'G')
{
secondBase = 2;
}
else if(newSequence[i+1] == 'U')
{
secondBase = 3;
}
if(newSequence[i+2] == 'A')
{
thirdBase = 0;
}
else if(newSequence[i+2] == 'C')
{
thirdBase = 1;
}
else if(newSequence[i+2] == 'G')
{
thirdBase = 2;
}
else if(newSequence[i+2] == 'U')
{
thirdBase = 3;
}
bool readSequence = true;
if (aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[0][3][2])
{
readSequence = true;
}
else if (aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[3][0][0] ||
aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[3][0][2] ||
aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[3][2][0])
{
readSequence = false;
}
if(readSequence)
{
protein = protein + aminoAcid[firstBase][secondBase][thirdBase] + " ";
}
else
{
continue;
}
}
return protein;
}
The bool is used for "start codons" and "stop codons", basically codons within the strand that will tell you when to record and when to stop. newSequence would be the RNA strand.
EDIT: I'm fairly new at this, so I understand my code may look really ugly. Any feedback on how to clean it up is much appreciated as well.
for(int i = 0; i < newSequence.length() - 2; i+3)
should be
for(int i = 0; i < newSequence.length() - 2; i += 3)
Your code never changes the value of i which is why it never stops running.
Your loop starts with the same piece of code three times, where you convert the letter to the 'base index'. That's an obvious place to use a function
for (int i = 0; i < newSequence.length() - 2; i += 3)
{
int firstBase = baseIndex(newSequence[i]);
int secondBase = baseIndex(newSequence[i + 1]);
int thirdBase = baseIndex(newSequence[i + 2]);
...
I'll leave you to write the baseIndex function.

how to use the switch statement for 3 characters at a time?

I have a string of different 3 letter words:
catdogmattabratliematdogatt
I need to assign each of the words with an alphabet and print the final string.
cat: P, dog: A, mat: T, tab: I, rat: E, lie: O, att: L
so my output should be:
PATIEOTAL
I tried using a switch statement but it doesn't allow me to enter 3 characters together.
Use a map<string, char> to do the following:
map<string, char> wordToChar;
// Load wordToChar appropriately; c++11 you can use an initializer list for simplicity
for (int i = 0; i < strLen; i += 3)
{
string str;
str.push_back(theString[i]);
str.push_back(theString[i+1]);
str.push_back(theString[i+2]);
cout << wordToChar[theString] << endl;
}
It seems I can use three char in a switch-statement! It is also not just me but a standard features called "multicharacter literals" (see 2.14.3 [lex.ccon] paragraph 1; the type of these beasts is int). It isn't that I would advise anybody to use this hack but it works although the characters may need to be reversed in the computation depending on the Endianess of the system (I'm not sure about that detail). Here is a complete example (although the input wants separate words rather than one string which needs to be separated):
#include <iostream>
#include <string.h>
int main(int ac, char* av[])
{
typedef unsigned char u;
for (int i(1); i != ac; ++i) {
if (strlen(av[i]) == 3)
{
std::cout << std::hex;
int value(u(av[i][2])
+ 256u * (u(av[i][1])
+ 256u * u(av[i][0])));
switch (value) {
default:
std::cout << "option not found!: '" << av[i] << "'\n";
break;
case 'cat': std::cout << 'P'; break;
case 'dog': std::cout << 'A'; break;
case 'mat': std::cout << 'T'; break;
case 'tab': std::cout << 'I'; break;
case 'rat': std::cout << 'E'; break;
case 'lie': std::cout << 'O'; break;
case 'att': std::cout << 'L'; break;
}
}
}
std::cout << '\n';
}
Since you asked about using switch statements, I assume that it would be acceptable to have a hard-coded list of 3-letter words and their corresponding letters. In that case, I would solve this problem using a sequence of if-then-else statements, each attempting to match the characters of the 3-letter words. Alternatively, you could use nested switch statements, but the syntax makes that solution a bit harder to read IMO.
static char match_word(std::string const &str, std::size_t offset)
{
char ret = '?';
if (str[offset + 0] == 'c' && str[offset + 1] == 'a' && str[offset + 2] == 't') {
ret = 'P';
}
else if (str[offset + 0] == 'd' && str[offset + 1] == 'o' && str[offset + 2] == 'g') {
ret = 'A';
}
else if (str[offset + 0] == 'm' && str[offset + 1] == 'a' && str[offset + 2] == 't') {
ret = 'T';
}
else if (str[offset + 0] == 't' && str[offset + 1] == 'a' && str[offset + 2] == 'b') {
ret = 'I';
}
else if (str[offset + 0] == 'r' && str[offset + 1] == 'a' && str[offset + 2] == 't') {
ret = 'E';
}
else if (str[offset + 0] == 'l' && str[offset + 1] == 'i' && str[offset + 2] == 'e') {
ret = 'O';
}
else if (str[offset + 0] == 'a' && str[offset + 1] == 't' && str[offset + 2] == 't') {
ret = 'L';
}
return ret;
}
Then you can test the code with a simple main function like this:
int main(int argc, char **argv)
{
if (argc != 2) {
std::cerr << "USAGE: " << argv[0] << " ENCODED" << std::endl;
return 1;
}
else {
std::string example(argv[1]);
for (std::size_t idx = 0; idx < example.size(); idx += 3) {
std::cout << match_word(example, idx);
}
std::cout << std::endl;
return 0;
}
}
Then just run the program with the encoded string as the one and only argument like this:
$ ./a.out catdogmattabratliematdogatt
PATIEOTAL

Confusing of "Access Violation "

I don't Understand with this case but this is really really important for me, Please Help me...
void __fastcall TForm1::Button4Click(TObject *Sender)
{
String masuk, keluar, kosong;
int i, x, j, n = 0;
masuk = Edit2->Text;
keluar = masuk;
kosong = " ";
n = 0;
x = 0;
mulai:
i = 1;
j = 0;
j = j + n;
i = i + j;
if (masuk[i] == 'a')
{
keluar[i] = 't';
}
else if (masuk[i] == 't')
{
keluar[i] = 'a';
}
else if (masuk[i] == 'c')
{
keluar[i] = 'g';
}
else if (masuk[i] == 'g')
{
keluar[i] = 'c';
}
else
{
Application->MessageBoxA("Masukan Anda Salah", "Peringatan", MB_OK | MB_ICONWARNING);
keluar = kosong;
goto end;
}
n = n + 1;
if (i < 10)
goto mulai;
else
goto end;
end:
Memo1->Text = keluar;
}
if I make masukan more than 10 (i<10 (10 as default value)), it is ok but if it less than 10, it will make message exception Class EAccessViolation..
Taking a shot in the dark, but I think what you're actually trying to do might be this. I'm assuming that you're taking a single string of 10 characters which represents one half of a genome and you're generating another string of the pair values.
void __fastcall TForm1::Button4Click(TObject *Sender)
{
String masuk, keluar;
masuk = Edit2->Text;
keluar = masuk;
char kosong = ' ';
for (int i=0; i < 10; i++)
{
switch(masuk[i]) {
case 'a':
keluar[i] = 't';
break;
case 't':
keluar[i] = 'a';
break;
case 'c':
keluar[i] = 'g';
break;
case 'g':
keluar[i] = 'c';
break;
default:
Application->MessageBoxA("Masukan Anda Salah", "Peringatan", MB_OK | MB_ICONWARNING);
keluar[i] = kosong;
break;
}
Memo1->Text = keluar;
}