I have a project where I build a dotmatrix module and then import it into another program that will print out my initials. I have it done to the point where it will print out my initials, but it won't print the "J" in the
initial = dotmatrix.dotJ("J")
It will just print the "*". I have defined in my module:
def dotJ(char):
"""Creates a capital J in 7 x 7 dots"""
dotJ = " * \n"
dotJ += " * \n"
dotJ += " * \n"
dotJ += " * \n"
dotJ += " * * \n"
dotJ += " * * \n"
dotJ += " *** \n"
return dotJ
where I have the *, I want it to print out whatever is called for in the
initial = dotmatrix.dotJ("J")
A simple way would be to define your dotJ variable with a star and then replace the star with the character:
def dotJ(char):
"""Creates a capital J in 7 x 7 dots"""
dotJ = " * \n"
dotJ += " * \n"
dotJ += " * \n"
dotJ += " * \n"
dotJ += " * * \n"
dotJ += " * * \n"
dotJ += " *** \n"
dotJ = dotJ.replace("*",char)
return dotJ
You could also use a format string. But I suspect this would be less easy to read.
Related
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name[] = {"Geeks ", "for", "Geeks"} ;
string v,s ;
v += name[0][0] + "." ;
cout << v << "\n" ;
s += name[0][0] ;
s += "." ;
cout << s << "\n" ;
}
On outputting:
string v outputs a garbage string while string s outputs G.
Output :
# // output of string v
G. // output of string s
Why does v give out a garbage value even though I'm appending '.' to the new string ?
Kindly explain both the cases.
Clang will complain about:
v += name[0][0] + "." ;
with a message like:
warning: adding 'std::...' (aka 'char') to a string does not append to the string [-Wstring-plus-int]
In other words, string + "chars" does not append the chars to the string, and so you need to create the string manually:
v += name[0][0] + string(".") ;
Why the following code returns an exception in the last line?
print(m)
print(b)
print(r)
print(p)
print(se)
print("m: " + m + "\n" + "b: " + b + "\n" + "r: " + r + "\n" + "p " + p + "\n" + "se " + se)
1516.13788561
-5731.63903831
0.858729519032
4.15127287882e-05
250.925294078
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-195-7e2d88a4b5a8> in <module>()
9 print(se)
10
---> 11 print("m: " + m + "\n" + "b: " + b + "\n" + "r: " + r + "\n" + "p " + p + "\n" + "se " + se)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
Try casting the variables as strings for the print function.
print("m: " + str(m) + "\n" + "b: " + str(b) + "\n" + "r: " + str(r) + "\n" + "p " + str(p) + "\n" + "se " + str(se))
I am writing a program that is heavily dependent on the perl's index function to take strings. My objective so to break this string into a series of substrings that do not have any spaces.
The problem that I am having is that in the second half of the loop the index function starts skipping spaces.
I am using the test data:
1 12 a 2 5 P Q
I am expecting to get:
1, 12, a, 2, 5, P, Q,
Instead I get:
1, 12, a, 2 5, P Q,
My code follows:
use 5.010;
use strict;
use warnings;
my $ivalue = <stdin>;
chomp($ivalue);
$ivalue = $ivalue . " ";
my $current;
my $space = 0;
my $safespace = 0;
my $lastspace = 0;
my $closestspace = 0;
my $i = 0;
# Test data - 1 12 a 2 5 P Q
while ($space != -1){
$space = index($ivalue, " ", $space + $i++);
$closestspace = $space - $lastspace;
#print $lastspace . " " . $space . " = " . $closestspace . "; ";
$current = substr($ivalue, $lastspace, $closestspace);
#say "substring = " . $current;
$lastspace = $space + 1;
}
Thanks ahead of time! If anyone has any suggestion on how to improve the way I asked my question or on my code in general those are appreciated as well.
The problem is in this line:
$space = index($ivalue, " ", $space + $i++);
instead of using $i, make that $space + 1
I have 3 files
base.txt
12345 6 78
13579 2 46
24680 1 35
123451 266 78
135792 6572 46
246803 12587 35
1stcheck.txt
Some odded stuffs
AB 12345/6/78 Fx00
BC 13579/2/47 0xFF
CD 24680/1/35 5x88
AB 123451/266_10/78 Fx00 #10 is mod(266,256)
BC 135792/6572_172/46 0xFF #172 is mod(6572,256)
CD 246803/12587_43/35 5x88 #43 is mod(12587,256)
There may be some other odded stuffs
2ndcheck.txt
12345u_6_78.dat
13579u_2_46.dat
24680u_0_35.dat
123451u_10_78.dat #10 is mod(266,256)
135792u_172_46.dat #172 is mod(6572,256)
246803u_43_35.dat #43 is mod(12587,256)
The info in 1stcheck.txt and 2ndcheck.txt is just combination of base.txt in applied some template/format
I'd like to have
report.txt
12345 6 78 passed passed
| |
(12345/6/78) (12345u_6_78)
13579 2 46 failed passed
24680 1 35 passed failed
123451 266 78 passed passed
135792 6572 46 passed passed
246803 12587 35 passed passed
Please help to consider about performance since
base.txt,2ndcheck.txt ~ 8MB-12MB
1stcheck.txt ~ 70MB
Many thanks
You'll have to decide if this is memory efficient: it does have to store data from all files in arrays before printing the table.
Required GNU awk
gawk '
# base file: store keys (and line numbers for output ordering)
FILENAME == ARGV[1] {key[$0] = FNR; next}
# 1st check: if key appears in base, store result as pass
FILENAME == ARGV[2] {
k = $2
gsub(/\//, " ", k)
if (k in key) pass1[k] = 1
}
# 2nd check: if key appears in base, store result as pass
FILENAME == ARGV[3] {
if ( match($0, /([0-9]+)._([0-9]+)_([0-9]+)\.dat/, m) ) {
k = m[1] " " m[2] " " m[3]
if (k in key) pass2[k] = 1
}
next
}
# print the result table
END {
PROCINFO["sorted_in"] = "#val_num_asc" # traverse array by line number
for (k in key) {
printf "%s\t%s\t%s\n", k \
, (k in pass1 ? "passed" : "failed") \
, (k in pass2 ? "passed" : "failed")
}
}
' base.txt 1stcheck.txt 2ndcheck.txt
12345 6 78 passed passed
13579 2 46 failed passed
24680 1 35 passed failed
Based on #glenn jackman's suggestion, I could solve my problem
gawk '
# Store key for 1st check
FILENAME == ARGV[1] {
k = $2
gsub(/\//, " ", k)
key_first[k];next
}
# Store key for 2nd check
FILENAME == ARGV[2] {
if ( match($0, /([0-9]+)._([0-9]+)_([0-9]+)\.dat/, m) ) {
k = m[1] " " m[2] " " m[3]
key_second[k];
}
next
}
# base file: do check on both 1st and 2nd check
FILENAME == ARGV[3] {
if($2>256) {
first=$1 " " $2 "_" ($2%256) " " $3
}
else {
first=$1 " " $2 " " $3
}
second=$1 " " $2%256 " " $3
if (first in key_first) pass1[$0] = 1
if (second in key_second) pass2[$0] = 1
key[$0]= FNR; next
}
# print the result table
END {
PROCINFO["sorted_in"] = "#val_num_asc" # traverse array by line number
for (k in key) {
printf "%s\t%s\t%s\n", k \
, (k in pass1 ? "sic_passed" : "sic_failed") \
, (k in pass2 ? "gd_passed" : "gd_failed")
}
}
' 1stcheck.txt 2ndcheck.txt base.txt
I need to use the function getline to be able to read in the spaces within a string. Currently, I'm reading word by word and any space will input the next word into a different variable. A small extract of the code is as below.
istream & operator >>( istream & input, Unit & C )
{
input >> C.unID >> C.unName >> C.credits >> C.Result >> C.mDay >> C.mMonth >> C.mYear;
return input;
}
ostream & operator <<( ostream & os, const Unit & C )
{
os << " Unit ID: " << C.unID << '\n';
os << " Unit Name: " << C.unName << '\n'
<< " Credits: " << C.credits << '\n'
<< " Result: " << C.Result << " marks" << '\n'
<< " Date: " << C.mDay << " " << C.mMonth << " " << C.mYear << '\n';
return os;
}
Note that I only need getline for unName.
As for the infile, it's in my main.cpp. Code are as below.
ifstream infile( "rinput.txt" );
if( !infile ) return -1;
Student R;
infile >> R;
ofstream ofile( "routput.txt" );
ofile << R
<< "Number of units = " << R.GetCount() << '\n'
<< "Total credits = " << R.GetCredits() << '\n';
The code works fine and all.
If I understand what you are trying to do, the issue is not with your code but with how you organize your input. In your code, if you have Unit::unName contain spaces while all other fields of Unit do not contain spaces, you need to parse your line yourself. The input has to be formatted properly. For example, if you use a delimiter such as ',' in your input file/stdin that does not appear in any valid unName or unId etc., you can use
istream & operator >>( istream & input, Unit & C ) {
input >> C.unID; getline(input,C.unName,','); input >> C.credits; ...
}
instead of,
istream & operator >>( istream & input, Unit & C )
{
input >> C.unID >> C.unName >> C.credits >> C.Result >> C.mDay >> C.mMonth >> C.mYear;
return input;
}
In above, the overloaded form istream& getline (istream& is, string& str, char delim); uses a delimiter such as ',' or '\t' to replace the default \n. You need to use format your input lines with delimiters because otherwise program will not be able to tell if 200 in
1 John Smith 200
should be part of the unName field or the credits field.
So, you can change this to
1, John Smith, 200, ...
and use ',' as the delimiter or change input to
1
John Smith
200
...
and use the default '\n' as the delimiter.