🤣

How laziness helps you

Posted on
Jan 18, 2024 06:34 AM
I was doing CS50x (by Harvard) Problem Set
notion image
as I’m lazy, I did not like to type 26 if-statement for this problem set
 
so I ask GPT to help me with bash-script
i have a csv file with two row of data. I want to print out else if ( word[i] == ROW1) { score += ROW2 }. how can I do it using a single bash command?
i have a csv file with two row of data. I want to print out else if ( word[i] == ROW1) { score += ROW2 }. how can I do it using a single bash command?
To achieve this in a single Bash command, you can use tools like awk or paste. Assuming your CSV file has two rows and you want to generate a series of else if conditions based on these rows, you can use the following command:
awk -F',' 'NR==1 {split($0, ROW1, ",")}; NR==2 {split($0, ROW2, ",")}; END {for(i=1; i<=length(ROW1); i++) print "else if (word[i] == " ROW1[i] ") { score += " ROW2[i] " }"}' your_file.csv
Replace your_file.csv with the actual path to your CSV file.
Explanation:
  • F',': Specifies that the field separator is a comma.
  • NR==1 {split($0, ROW1, ",")}: If it's the first row (NR is the record number), split the row into elements using a comma and store the result in the array ROW1.
  • NR==2 {split($0, ROW2, ",")}: If it's the second row, split the row into elements using a comma and store the result in the array ROW2.
  • END {...}: After processing all rows, execute the following block.
  • for(i=1; i<=length(ROW1); i++) print "else if (word[i] == " ROW1[i] ") { score += " ROW2[i] " }": Loop through the elements of ROW1 and ROW2 and print the desired else if conditions.
This command assumes that your CSV file is well-formed, and each row has the same number of elements. It may need adjustments based on your specific CSV structure.
and with some RegEx, I make it more pretty and userful
Final code
for(int i = 0, n = strlen(word); i < n; i++) { if (word[i] == 'A' || word[i] == 'a') { score += 1; } else if (word[i] == 'B' || word[i] == 'b') { score += 3; } else if (word[i] == 'C' || word[i] == 'c') { score += 3; } else if (word[i] == 'D' || word[i] == 'd') { score += 2; } else if (word[i] == 'E' || word[i] == 'e') { score += 1; } else if (word[i] == 'F' || word[i] == 'f') { score += 4; } else if (word[i] == 'G' || word[i] == 'g') { score += 2; } else if (word[i] == 'H' || word[i] == 'h') { score += 4; } else if (word[i] == 'I' || word[i] == 'i') { score += 1; } else if (word[i] == 'J' || word[i] == 'j') { score += 8; } else if (word[i] == 'K' || word[i] == 'k') { score += 5; } else if (word[i] == 'L' || word[i] == 'l') { score += 1; } else if (word[i] == 'M' || word[i] == 'm') { score += 3; } else if (word[i] == 'N' || word[i] == 'n') { score += 1; } else if (word[i] == 'O' || word[i] == 'o') { score += 1; } else if (word[i] == 'P' || word[i] == 'p') { score += 3; } else if (word[i] == 'Q' || word[i] == 'q') { score += 10; } else if (word[i] == 'R' || word[i] == 'r') { score += 1; } else if (word[i] == 'S' || word[i] == 's') { score += 1; } else if (word[i] == 'T' || word[i] == 't') { score += 1; } else if (word[i] == 'U' || word[i] == 'u') { score += 1; } else if (word[i] == 'V' || word[i] == 'v') { score += 4; } else if (word[i] == 'W' || word[i] == 'w') { score += 4; } else if (word[i] == 'X' || word[i] == 'x') { score += 8; } else if (word[i] == 'Y' || word[i] == 'y') { score += 4; } else if (word[i] == 'Z' || word[i] == 'z') { score += 10; } }
and I was done