Maxima: suppress `linenum` in both input and output prompt - prompt

The Maxima documentation reads:
Maxima automatically constructs a label for each computed expression by concatenating outchar and linenum.
http://maxima.sourceforge.net/docs/manual/maxima_4.html#outchar
It describes how to customize the inchar and outchar, but there is always the line number included. I know this is useful for referencing expressions later, but when I am putting the post on e.g. StackOverflow, the line numbers aren't usually important, so I want to omit them.
(Also, this is purely cosmetic, but sometimes it occasionally makes the inputs misalign a little.)
Instead of this:
(%i9) 1 + 1;
(%o9) 2
(%i10) 2 + 2;
(%o10) 4
I want this:
(%i) 1 + 1;
(%o) 2
(%i) 2 + 2;
(%o) 4
How can I customize the Maxima prompt to suppress the line number?

Related

How to 'manipulate' strings in BASIC V2?

I would like to reach the following:
I ask for a number from the user, and then output a string like the following:
-STR$
--STR$
---STR$
----STR$
-----STR$
I tried to do this:
10 INPUT NUM%
20 FOR X=1 TO NUM%: PRINT NUM%*"-" + "TEXT" : NEXT
The code above got me an error: ?TYPE MISMATCH EROR IN 20
However, I didn't yet figure out how to manipulate the string's beginning to multiply the '-' marks on each loop run
Maybe this:
10 INPUT NUM%
20 FOR I = 1 TO NUM%
30 FOR J = 1 TO I: PRINT "-"; : NEXT
40 PRINT " TEXT"
50 NEXT
There is no multipy of strings/character, as far as I remember to old (good) times.
I believe even older, more primitive forms of BASIC had the STRING$() function. It takes two parameters: the number of times to repeat the character and the character itself. So...
10 INPUT NUM%
20 FOR X=1 TO NUM%: PRINT STRING$(NUM%, "-") + "TEXT" : NEXT
An alternative:
100 INPUT NM%
110 BR$="----------"
120 PRINT LEFT$(BR$,NM%);
130 PRINT "TEXT"
This eliminates the need for an expensive FOR loop, and should be okay as long as NM% is not greater than the length of BR$.
One other thing to point out is that your variable names are effectively capped at two characters, e.g.:
The length of variable names are optional, but max. 80 chars (logical input line of BASIC). The BASIC interpreter used only the first 2 chars for controlling the using variables. The variables A$ and AA$ are different, but not AB$ and ABC$.
(Source: https://www.c64-wiki.com/wiki/Variable). For that reason I used NM% instead of NUM%; it will prevent issues later.

Replace the first field with values from a mapping

I have some data (basically bounding box annotations) in a txt files (space separated)
I would like to replace multiple occurrences of specific characters with some other characters. For example
0 0.649489 0.666668 0.0625 0.260877
1 0.89485 0.445085 0.0428084 0.084259
1 0.80625 0.508509 0.0469892 0.005556
2 0.529068 0.0906668 0.0582908 0.0954804
2 0.565625 0.0268509 0.0040625 0.0546296
I might have to change it to something like
2 0.649489 0.666668 0.0625 0.260877
4 0.89485 0.445085 0.0428084 0.084259
4 0.80625 0.508509 0.0469892 0.005556
7 0.529068 0.0906668 0.0582908 0.0954804
7 0.565625 0.0268509 0.0040625 0.0546296
and this should happen simultaneously for all the elements only in the first column (not one after the other replacement as that will index it incorrectly)
I'll basically have a mapping {old_class_1:new_class_1,old_class_2:new_class_2,old_class_3:new_class_3} and so on...
I looked into the post here, but it does not work for my case since the method described in those answers would change all the values to the last replacement.
I looked into this post as well, but am not sure if the answer here can be applied to my case since I'll have around 25 classes, so the indexes (the values of the first column) can range from 0-24
I know this can be probably be done in python by reading each file line by line and making the replacement, just was wondering if there was a quicker way
Any help would be appreciated. Thanks!
Here's a simple example of how to map the labels in the first column to different ones.
This specifies the mapping as a variable; you could equally well specify it in a file, or something else entirely. The main consideration is that you need to have unambiguous separator characters, and use a format which isn't unnecessarily hard for Awk to parse.
awk 'BEGIN { n = split("0:2 1:4 2:7", m);
for(i=1; i<=n; ++i) { split(m[i], p); map[p[1]] = p[2] } }
$1 in map { $1 = map[$1] }1' file
The BEGIN field could be simplified, but I wanted to make it easy to update; now all you have to do is update the string which is the first argument to the first split to specify a different mapping. We spend a bunch of temporary variables on parsing out the values into an associative array map which is what the main script then uses.
The final 1 is not a typo; it is a standard Awk idiom to say "print every line unconditionally".

Python ord() and chr()

I have:
txt = input('What is your sentence? ')
list = [0]*128
for x in txt:
list[ord(x)] += 1
for x in list:
if x >= 1:
print(chr(list.index(x)) * x)
As per my understanding this should just output every letter in a sentence like:
))
111
3333
etc.
For the string "aB)a2a2a2)" the output is correct:
))
222
B
aaaa
For the string "aB)a2a2a2" the output is wrong:
)
222
)
aaaa
I feel like all my bases are covered but I'm not sure what's wrong with this code.
When you do list.index(x), you're searching the list for the first index that value appears. That's not actually what you want though, you want the specific index of the value you just read, even if the same value occurs somewhere else earlier in the list too.
The best way to get indexes along side values from a sequence is with enuemerate:
for i, x in enumerate(list):
if x >= 1:
print(chr(i) * x)
That should get you the output you want, but there are several other things that would make your code easier to read and understand. First of all, using list as a variable name is a very bad idea, as that will shadow the builtin list type's name in your namespace. That makes it very confusing for anyone reading your code, and you even confuse yourself if you want to use the normal list for some purpose and don't remember you've already used it for a variable of your own.
The other issue is also about variable names, but it's a bit more subtle. Your two loops both use a loop variable named x, but the meaning of the value is different each time. The first loop is over the characters in the input string, while the latter loop is over the counts of each character. Using meaningful variables would make things a lot clearer.
Here's a combination of all my suggested fixes together:
text = input('What is your sentence? ')
counts = [0]*128
for character in text:
counts[ord(character)] += 1
for index, count in enumerate(counts):
if count >= 1:
print(chr(index) * count)

Finding the Minimum Window in S which Contains All Elements from T

I have a specific question about the code in the problem and its solution at http://articles.leetcode.com/finding-minimum-window-in-s-which
In the code below the figure (NOT the figure above the figure), 5th line of code from for loop
if (hasFound[S[end]] <= needToFind[S[end]]) **// WHY this condition is required???**
count++;
Based on my understanding
1) this if condition is not needed and just (whenever found a char just increase count which represents the # of chars found so far)
count++;
2) OR could be < (instead of <=) and equal doesn't seem to make sense for me
if (hasFound[S[end]] < needToFind[S[end]])
count++;
I tested 1) and 2) but neither of them give me the correct answer for all cases.
only (if condition with <=) gives me the correct solution for all cases.
I really don't understand why
if (hasFound[S[end]] <= needToFind[S[end]])
should be required to make this code work correctly for all cases.
Imagine that pattern T contains 3 chars A and 2 chars B.
So needToFind['A'] = 3
And you have to increment count, when hasFound['A'] becomes 1, 2 and 3
When hasFound['A'] becomes 2 only (you proposition about '<'), window contains only two A chars, it is impossible to make T, and count never reaches tLen=5
If remove this condition at all, 5 chars A give count=5=tlen, while window still doesn't contain any B

perl interpolate code

I know in perl you can interpolate scalars by simply doing this:
"This is my $string"
However, I'm wondering if there is a way where I can interpolate actual perl code to be evaluated? An idea of what I want can be seen with ruby strings:
"5 + 4 = #{5 + 4}"
And it will evaluate whatever is in between the {}.
Does anyone know of a way to do this in perl? Thanks!
You can use the following trick:
"5 + 4 = #{[ 5 + 4 ]}"
Alternatively, you can use sprintf:
sprintf("5 + 4 = %d", 5 + 4);
Either of these yields the desired string. Arguably, sprintf is safer than the first one, as you can restrict the type of the interpolated value somewhat. The first one is closer in spirit to what you desire, however.
Further reading:
Why does Perl evaluate code in ${…} during string interpolation?
If you want to interpolate computations in string, you can do 3 things:
Use #{[]}. Inside the third brackets, place whatever expression you want.
Use ${\ do {}}. Place your expression within the 2nd braces.
Use a simple string concatenation: $targetstring = "$string1".func()."$string2", where func() will contain the dynamic content.
print "5 + 4 = ", 5 + 4;
Simply move it outside the quotes. Is there a point in forcing it to be within the quotes?
You can do a bit more formatting with printf, e.g.:
printf "5 + 4 = %s\n", 5 + 4;
You can use eval:
eval('$a = 4 + 5');
Mind you, eval should be used sparingly, it is pretty unsafe.

Resources