Related
A simple command here gave me a confusing output:
I have to create a list of a number of sequences.
For converting a fasta text file with "/n"s and titles in between (marked with ">") to a list of strings seperated only where a new title begins I tried:
x=int(0)
while x<len(my_enzyme_list):
if my_enzyme_list[x].startswith(">"):
x += 2
else:
my_enzyme_list[x-1:x] = "".join(my_enzyme_list[x-1:x])
x += 1
and it gave me (scroll till the end):
['>human_NAPRT Q6XQN6-1\n', 'MAAEQDPEARAAARPLLTDLYQATMALGYWRAGRARDAAEFELFFRRCPFGGAFALAAGLRDCVRFLRAFRLRDADVQFLASVLPPDTDPAFFEHLRALDCSEVTVRALPEGSLAFPGVPLLQVSGPLLVVQLLETPLLCLVSYASLVATNAARLRLIAGPEKRLLEMGLRRAQGPDGGLTASTYSYLGGFDSSSNVLAGQLRGVPVAGTLAHSFVTSFSGSEVPPDPMLAPAAGEGPGVDLAAKAQVWLEQVCAHLGLGVQEPHPGERAAFVAYALAFPRAFQGLLDTYSVWRSGLPNFLAVALALGELGYRAVGVRLDSGDLLQQAQEIRKVFRAAAAQFQVPWLESVLIVVSNNIDEEALARLAQEGSEVNVIGIGTSVVTCPQQPSLGGVYKLVAVGGQPRMKLTEDPEKQTLPGSKAAFRLLGSDGSPLMDMLQLAEEPVPQAGQELRVWPPGAQEPCTVRPAQVEPLLRLCLQQGQLCEPLPSLAESRALAQLSLSRLSPEHRRLRSPAQYQVVLSERLQALVNSLCAGQSP\n', '>XP_025969437.1 nicotinate phosphoribosyltransferase isoform X2 [Dromaius novaehollandiae]\n', 'M', 'A', 'L', 'L', 'T', 'D', 'L', 'Y', 'Q', 'V', 'T', 'M', 'A', 'Y', 'G', 'Y', 'W', 'R', 'A', 'G', 'R', 'H', 'R', 'V', 'P', 'A', 'A', 'A', 'E', 'L', 'F', 'F', 'R', 'R', 'C', 'P', 'F', 'R', 'G', 'A', 'F', 'A', 'L', 'G', 'A', 'G', 'L', 'A', 'E', 'G', 'L', 'R', 'F', 'L', 'R', 'A', 'F', 'R', 'F', 'S', 'A', 'A', 'D', 'I', 'A', 'Y', 'L', 'R', 'S', 'V', 'L', 'P', 'S', 'T', 'T', 'E', 'E', 'D', 'F', 'F', '\n', 'E', 'Y', 'L', 'A', 'T', 'V', 'D', 'A', 'S', 'E', 'V', 'T', 'I', 'S', 'S', 'V', 'P', 'E', 'G', 'S', 'V', 'V', 'F', 'S', 'R', 'V', 'P', 'L', 'L', 'Q', 'V', 'K', 'G', 'P', 'L', 'L', 'V', 'V', 'Q', 'L', 'L', 'E', 'T', 'T', 'L', 'L', 'C', 'L', 'V', 'S', 'Y', 'A', 'S', 'L', 'V', 'A', 'T', 'N', 'A', 'A', 'R', 'F', 'R', 'L', 'L', 'A', 'G', 'P', 'A', 'T', 'K', 'L', 'M', 'E', 'M', 'G', 'L', 'R', 'R', 'A', '\n', 'Q', 'G', 'P', 'D', 'G', 'G', 'L', 'S', 'A', 'S', 'K', 'Y', 'S', 'Y', 'I', 'G', 'G', 'F', 'D', 'C', 'T', 'S', 'N', 'V', 'L', 'A', 'G', 'K', 'L', 'Y', 'G', 'I', 'P', (and so on)
what happened?
I have this nested list in my Output:
[['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'], ['O',
'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'], ['O', 'O',
'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']]
How can I bring it in this form:
[
['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'],
['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'],
['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
]
In other words: How can I make a wrap in a for-loop ?
For example:
for i in range(3):
my_list.append(i)
# How to make now a wrap ?
pprint.pprint gives a nicely formatted output:
>>> L = [['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'], ['O',
'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'], ['O', 'O',
'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']]
>>> import pprint
>>> pprint.pprint(L)
[['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'],
['O', 'O', 'X', 'O', 'O', 'O', 'X', 'O', 'O', 'O', 'X', 'O'],
['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']]
Currently having an issue with the following code returning an array with symbols appended to the end. This is the smallest I could get the code in order to reproduce the error. What is causing this issue? I assume that maybe the array is getting numbers in it somehow which get interpreted as ascii symbols, but I can't figure out where this is happening.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int numRow = 6;
const int numCol = 26;
char letters[numRow][numCol] = {
{ 'm', 'w', 'r', 'u', 't', 'v', 'n', 'j', 'd', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'n', 'x', 'm', 'd', 'q', 'y', 'u', 't' },
{ 'y', 'e', 'r', 'y', 'e', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g', 'd', 'b', 'b', 'b', 'g', 'x', 'z' },
{ 'j', 'd', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'n', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g' },
{ 'y', 'e', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'g', 'a', 'a', 'g', 'd', 'b' },
{ 'e', 'r', 'y', 'e', 't', 't', 'v', 'n', 'j', 'd', 'j', 'y', 'k', 'w', 'r', 's', 'f', 'h', 's', 'g', 'g', 'g', 'd', 'c', 'v', 'g' },
{ 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g', 'd', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h' }
};
int main()
{
char *ltrptr;
ltrptr = &letters[0][0];
const int arraySize = 6 * 26;
int answer = 0;
cout << " Select row for sort: " << endl;
cin >> answer;
char newArray[numCol];
char *ltrptr2;
ltrptr2 = &newArray[0];
for (int i = 0; i < numCol; i++){
newArray[i] = letters[answer - 1][i];
}
cout << "Selected row: before" << newArray << endl;
selectionSort(ltrptr2, numCol, ascending);
cout << "Selected row: after " << newArray << endl;
getchar();
return 0;
}
It would be better if you actually included what output you got and what output you expected (and also the input given), rather than trying to describe the output.
But your code has an obvious error which is consistent with your vague description: you haven't put a C string in your character array, but you try to print it as if it did contain a C string.
(in particular, to store a C string in a character array, one must store the sequence of characters followed by a null character)
I am currently attempting to iterate through a 2d array to a function using pointer notation as an exercise. I found an example of how to do this on these forums; it's the if statement within the displayTable function. My compiler is giving me errors about the function call itself stating that the if statement from the displayTable function must be of a pointer type. Why is this not working? The example was up voted.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int numRow = 6;
const int numCol = 26;
char letters[numRow][numCol] = {
{ 'm', 'w', 'r', 'u', 't', 'v', 'n', 'j', 'd', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'n', 'x', 'm', 'd', 'q', 'y', 'u', 't' },
{ 'y', 'e', 'r', 'y', 'e', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g', 'd', 'b', 'b', 'b', 'g', 'x', 'z' },
{ 'j', 'd', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'n', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g' },
{ 'y', 'e', 't', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h', 'j', 'y', 'k', 'k', 'g', 'g', 'd', 'c', 'v', 'g', 'a', 'a', 'g', 'd', 'b' },
{ 'e', 'r', 'y', 'e', 't', 't', 'v', 'n', 'j', 'd', 'j', 'y', 'k', 'w', 'r', 's', 'f', 'h', 's', 'g', 'g', 'g', 'd', 'c', 'v', 'g' },
{ 'y', 'u', 'w', 'r', 's', 'f', 'h', 's', 'g', 's', 'f', 'h', 's', 'g', 'a', 'a', 'g', 'd', 'w', 'y', 'u', 'w', 'r', 's', 'f', 'h' }
};
void displayTable(char[][26]);
int main()
{
char *ltrptr = 0;
ltrptr = &letters[0][0];
const int arraySize = 6 * 26;
int answer = 0;
char * arr[6][26];
displayTable(letters);
getchar();
return 0;
}
void displayTable(char ans[][26]){
//pas 2d array, then point to it
cout << " The table as it stands: " << endl;
char * ans1;
ans1 = &ans[0][0];
for (char * iter = &ans1[0][0]; iter != &ans1[0][0] + 6 * 26; iter++){
cout << &ans1[0][0] << endl;
}
}
The problem is that your ans1 is a simple pointer-to-char and you're trying to dereference it twice. The correct way to do the loop would be
for (char * iter = ans1; iter != ans1 + 6 * 26; iter++){
cout << *iter << endl;
}
The output now is HHHHHHHHHHHHHHHHHHHH. What I would like the program to do is randomNum to have a different value each time it goes through the loop. so it would be like AacCEe... And goes on.
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main(){
srand(time(0));
cout << "You'r PW is: \t" << endl;
char abc [] {'A', 'a', 'B' ,'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k'};
int randomNum = rand() % 22;
for(int i = 0; i <20; i++){
cout << abc[randomNum];
}
}
You have Initialized randomNum before you loop
int randomNum = rand() % 22;
You should change your code to
int randomNum;
for(int i = 0; i <20; i++)
{
randomNum = rand() % 22;
cout << abc[randomNum];
}
This should work.
Currently randomNum gets only one value and you use that value throughout your loop without the value changing.
In the loop your printing the same character 20 times effectively, what you intended I suppose is randomly select 20 different characters. Look into my inlined comment.
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main(){
srand(time(0));
cout << "You'r PW is: \t" << endl;
char abc [] {'A', 'a', 'B' ,'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k'};
for(int i = 0; i <20; i++){
cout << abc[rand() % 22]; // look here
}
}