I have this code in lua (sorry if its bad).
function splitIntoTable(inputstr,sep)
local t = {}
for str in string.gmatch(inputstr,"([^" .. sep .. "]+)") do
table.insert(t,str)
end
return t
end
function displayList(table)
for k, v in ipairs(table) do
print(table[k])
end
end
local tocalc = "57 + 38"
print("Inputted: " .. tocalc)
tocalc = "0 " .. tocalc
local workwith = splitIntoTable(tocalc," ")
local did = 0
local doing = 1
local lenOfWorkwith = 0
for k in pairs(workwith) do
lenOfWorkwith = lenOfWorkwith + 1
end
repeat
if workwith[doing] == "+" then
did = did + workwith[doing - 1] + workwith[doing + 1]
end
doing = doing + 1
until doing > lenOfWorkwith
did = math.floor(did + 0.5)
print("Result: " .. did)
I know it's a bit inefficient, but I just need it usable right now. Basically, what its supposed to do is simply plus numbers. For example, I put in 57 + 38, it works fine and gives me the correct calculation, but as soon as I put in 3 numbers (for example, 57 + 38 + 40), it breaks down and doesn't give the correct answer.
You can simplify this significantly by using load or loadstring depending on your Lua version.
local tocalc = "57 + 38 + 40"
print("Result: " .. load("return " .. tocalc)())
Your algorithm is adding the middle term an additional time.
if workwith[doing] == "+" then
did = did + workwith[doing - 1] + workwith[doing + 1]
end
Here on the first "+" you will be did + 57 + 38, so did will be 95. on the next "+" you will have get did + 38 + 40, causing 38 to be added to the final value twice. To fix this you should simply look at the numbers and add them individually not in pairs.
repeat
if workwith[doing] ~= "+" then
did = did + workwith[doing]
end
doing = doing + 1
until doing > lenOfWorkwith
The algorithm still has other issues, I strongly suggest using solution using load I described above.
Related
So I have been given the following expression, but I cannot seem to solve it, can anyone do this and show the steps please?
Prove XY'Z + XYZ' + XYZ = XY + XZ
XY'Z + XYZ' + XYZ = XY + XZ
Notice X and Z are common factors between XY'Z and XYZ.
XZ(Y' + Y) + XYZ' =
Y' + Y is equal to 1 (if Y=0 then Y'=1 and so 0 + 1 = 1, that is 0 or 1 = 1. Similarly, if Y=1 then Y'=0 and so 1 + 0 = 1). Therefore, what you get is:
XZ·1 + XYZ' =
XZ·1 = XZ since A·1 = A (if A=0 then 0·1 is 0 and if A=1 then 1·1 = 1). Now the function is simplified to:
XZ + XYZ' =
Notice once again X is a common factor between XZ and XYZ'.
X(Z + YZ') =
Notice this time that Z + YZ' is a special case of the distributive law, which is A + A'B = A + B. This is because if we apply the general distributive law A + BC = (A + B)·(A + C) then we get A + A'B = (A + A')·(A + B) = 1·(A + B) = A + B. Following this reasoning we get to simplify the function even further:
X(Z + Y) =
All that's left is for us to use the distributive law and we finally arrive to the final result:
XY + XZ
Please note that nothing is written between variables, an AND operator (or "·" symbol) is assumed. It's just a way to save space.
I have this program that, when run, has prints in this pattern:
a(0) = 0
a(1) = 1
a(2) = 2 + a(1) = 3
a(3) = 3 + a(2) + a(1) = 3 + 3 + 1 = 7
a(4) = 4 + 3 + 3 + 1 = 15
At this point I observe that there is a pattern of it becoming O(2^n - 1). However, I am not sure if this is a valid proof by induction. An idea I had was:
a(n)= n + a(n-1) + a(n-2) + ... + 1 = 2^n - 1
But from here the pattern for the terms are not very clear to me. I know the first term is n (in our code, this is due to a for loop that prints a statement n times), and due to recursion I know I will be summing the previous values but I don't know what a(n-1), a(n-2), etc. are in terms of n.
How can I write a Vigenère encryption in Qbasic without the use of arrays?
I understand the math to encrypt a message:
Ca = Ma + Kb (mod 26)
And to decrypt a message:
Ma = Ca – Kb (mod 26).
I'm struggling with the syntax as I haven't found much information online.
You can solve this easily without using any arrays.
Below is my all-(Q)BASIC solution.
The MID$ function extracts one character from a string, and the ASC function converts the character into its ASCII code. Subtracting 65 produces a number in the range [0,25]. The encrypted number is turned back into a character using the CHR$ function. Hereafter the MID$ statement is used to put the encrypted character back in the string.
Because of the different lengths between the message and the encryption key, a separate iteration variable (j%) is needed to repeatedly walk through the key string.
msg$ = "ENCRYPTION"
PRINT msg$
key$ = "CLINTON"
k% = LEN(key$)
j% = 1
FOR i% = 1 TO LEN(msg$)
a% = (ASC(MID$(msg$, i%, 1)) - 65) + (ASC(MID$(key$, j%, 1)) -65)
MID$(msg$, i%) = CHR$(65 + a% + 26 * (a% > 25))
j% = j% + 1 + k% * (j% = k%)
NEXT i%
PRINT msg$
The above snippet could do without one - 65 and one + 65, but I've left these in for clarity.
The decryption process is quite similar. 3 little changes are all that it takes:
j% = 1
FOR i% = 1 TO LEN(msg$)
a% = (ASC(MID$(msg$, i%, 1)) - 65) - (ASC(MID$(key$, j%, 1)) -65)
MID$(msg$, i%) = CHR$(65 + a% - 26 * (a% < 0))
j% = j% + 1 + k% * (j% = k%)
NEXT i%
PRINT msg$
Running both snippets in a row produces:
ENCRYPTION
GYKERDGKZV
ENCRYPTION
What about a version of the code that can deal with spaces, punctuation marks, and accented characters?
The code is very similar and even a bit simpler:
msg$ = "This is any text that needs encrypting. So sayeth Sep Roland!"
PRINT msg$
key$ = "Blaise de Vigenère"
k% = LEN(key$)
j% = 1
FOR i% = 1 TO LEN(msg$)
a% = ASC(MID$(msg$, i%, 1)) + ASC(MID$(key$, j%, 1))
MID$(msg$, i%) = CHR$(a% + 256 * (a% > 255))
j% = j% + 1 + k% * (j% = k%)
NEXT i%
PRINT msg$
j% = 1
FOR i% = 1 TO LEN(msg$)
a% = ASC(MID$(msg$, i%, 1)) - ASC(MID$(key$, j%, 1))
MID$(msg$, i%) = CHR$(a% - 256 * (a% < 0))
j% = j% + 1 + k% * (j% = k%)
NEXT i%
PRINT msg$
I will not reproduce any output here because that would be a real pita...
Are these strange embedded conditions correct?
(a% + 26 * (a% > 25))
Consider the equivalent simple code:
IF a% > 25 THEN
a% = a% - 26
ENDIF
If the a% variable is greater than 25, we need to subtract 26.
Nonetheless the (a% + 26 * (a% > 25)) form uses addition.
This so happens because a TRUE condition evaluates to -1.
If a% > 25 is TRUE we get (a% + 26 * -1) -> a% - 26
If a% > 25 is FALSE we get (a% + 26 * 0) -> a%
You can simply get the ASCII value of the char as a number and then subtract the character value of A. You would get a number in the range [0, 26). Then you'd perform encryption / decryption as you've stated. To get back a valid character value then reverse and add the value of A. This works because the letters of the English alphabet (the ABC) are listed in order in ASCII.
To get the ciphertext or plaintext simply iterate over all the characters in the string (possibly after checking that it doesn't contain any other characters) and append the encrypted / decrypted character to a new string, and finally return that. Viola, no arrays, just strings, characters and numerical values.
That's all folks.
This question is about humane representation of Maxima output.
In short, how do I make
b*d4 + b*d3 + b*d2 + b*d1 + a*sin(c4 + alpha) + a*sin(c3 + alpha) + a*sin(c2 + alpha) + a*sin(c1 + alpha)
look like
b*sum_{i=1}^{4} d_i + a*sum_{j=1}^{4}sin(c_i + \alpha)
where sum_{*}^{*}* is a summation sign and an expression with subscripts ?
Or deeper, how to properly model a finite set of items here ?
Consider a finite set of entities $x_i$ (trying to speak tex here) that are numbered from 1 to n where n is known. Let a function $F$ depend on several characteristics of those entities $c_ji = c_j(x_i), j = 1..k$ (k - also known) so that $F = F(c_11,...,c_kn)$.
Now when I try to implement that in Maxima and do things with it, it would yield sums and products of all kinds, where the numbered items are represented something like $c_1*a + c_2*a + c_3*a + c_4*a + d_1*b + d_2*b + d_3*b + d_4*b$ which you would write down on paper as $a*\sum_{i=1}^{4}c_i + b*sum_{i=1}^{4}d_i$.
So how can I make Maxima do that sort of expression contraction ?
To be more specific, here is an actual code example:
(Maxima output marked as ">>>")
/* let's have 4 entities: */
n: 4 $
/* F is a sum of similar components corresponding to each entity F = F_1 + F_2 + F_3 + F_4 */
F_i: a*sin(alpha + c_i) + b*d_i;
>>> b*d_i + a*sin(c_i + alpha)
/* defining the characteristics */
c(i) := concat(c, i) $
d(i) := concat(d, i) $
/* now let's see what F looks like */
/* first, we should model the fact that we have 4 entities somehow: */
F_i(i) := subst(c(i), c_i, subst(d(i), d_i, F_i)) $
/* now we can evaluate F: */
F: sum(F_i(i), i, 1, 4);
>>> b*d4 + b*d3 + b*d2 + b*d1 + a*sin(c4 + alpha) + a*sin(c3 + alpha) + a*sin(c2 + alpha) + a*sin(c1 + alpha)
/* at this point it would be nice to do something like: */
/* pretty(F); */
/* and get an output of: */
/* $b*\sum_{i=1}^{4}d_i + a*\sum_{j=1}^4 sin(c_j + \alpha)$ */
/* not to mention having Maxima write things in the same order as I do */
So, to sum up, there are three quetions here:
How do I factor out a sum from an expression like the one on top of this post ?
How do I properly let Maxima know what I'm speaking about here ?
How to make Maxima preserve my order of things in output ?
Thanks in advance.
Here's a way to go about what I think you want.
(%i1) n: 4 $
(%i2) F(i) := a*sin(alpha + c[i]) + b*d[i];
(%o2) F(i) := a sin(alpha + c ) + b d
i i
(%i3) 'sum(F(i), i,1,4);
4
====
\
(%o3) > (a sin(c + alpha) + b d )
/ i i
====
i = 1
(%i4) declare (nounify(sum), linear);
(%o4) done
(%i5) 'sum(F(i), i,1,4);
4 4
==== ====
\ \
(%o5) a > sin(c + alpha) + b > d
/ i / i
==== ====
i = 1 i = 1
(%i6)
The most important thing here is that I've written what we would call "c sub i" and "d sub i" as c[i] and d[i] respectively. These are indexed variables named c and d, and i is the index. It's not necessary for there to actually exist arrays or lists named c or d, and i might or might not have a specific value.
I have written F as an ordinary function. I've avoided the construction of variable names via concat and avoided the substitution of those names into an expression. I would like to emphasize that such operations are almost certainly not the best way to go about it.
In %i3 note that I wrote the summation as 'sum(...) which makes it a so-called noun expression, which means it is maintained in a symbolic form and not evaluated.
By default, summations are not treated as linear, so in %i4 I declared summations as linear so that the result in %o5 is as expected.
Maxima doesn't have a way to collect expressions such as a1 + a2 + a3 back into a symbolic summation, but perhaps you don't need such an operation.
I'd like to decode this string:
X-OVH-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrfeelgedrvdduucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfenuceurghilhhouhhtmecufedttdenucgohfhorhgsihguuggvnhfjughrucdlhedttddm
How can I do this?
There is a Tor hidden service you can use to decode the tag located at http://6jbnmws2zq2m2fsfmpwnssgsrxovohgggphymkd4df2pgcw7ccrdy6ad.onion
According to it, the X-OVH-SPAMCAUSE you gave translates to this:
Vade Retro 01.394.21 AS+AV+AP+RT Profile: OVH; Bailout: 300; ^ForbiddenHdr (500)
Starting from lkraider's great Python answer, I improved the accuracy. It turns out that the offset characters (c..g) are alternately appended and prepended. So instead of just checking if one of them is in the pair, it is necessary to differentiate between, e.g., fh and hf, by keeping track of even or odd pairs.
def decode(msg):
text = ""
for i in range(0, len(msg), 2):
# add position as extra parameter
text += unrot(msg[i: i + 2], i // 2)
return text
def unrot(pair, pos, key=ord('x')):
# "even" position => 2nd char is offset
if pos % 2 == 0:
# swap letters in pair
pair = pair[1] + pair[0]
# treat 1st char as offset
offset = (ord('g') - ord(pair[0])) * 16
# map to original character
return chr(sum(ord(c) for c in pair) - key - offset)
print(decode('gggruggvucftvghtrhho'))
https://gist.github.com/DoubleYouEl/e3de97293ce3d5452b3be7a336a06ad7
Looks to be some obfuscation by rotating chars. I made an attempt at it using Python. It's not perfect but mostly seems to work:
def decode(msg):
text = []
for i in range(0, len(msg), 2):
text.append(unrot(msg[i: i + 2]))
return str.join('', text)
def unrot(pair, key=ord('x')):
offset = 0
for c in 'cdefgh':
if c in pair:
offset = (ord('g') - ord(c)) * 16
break
return chr(sum(ord(c) for c in pair) - key - offset)
print(decode('gggruggvucftvghtrhho'))
https://gist.github.com/lkraider/9530798a695586fc1580d0728966f6f0
I improved the given Python solutions by Ikraider and DoubleYou and added a JavaScript solution, too.
Python:
def Decode(msg):
return ''.join([chr(ord(msg[i * 2]) + ord(msg[i * 2 + 1]) - 1768 + ord(msg[i * 2 + 1 - (i & 1)]) * 16) for i in range(len(msg) // 2)])
print(Decode('gggruggvucftvghtrhho'))
JavaScript:
function Decode(msg)
{
return Array(msg.length >> 1).fill(0).map((_, i) => String.fromCharCode(msg[i * 2].charCodeAt(0) + msg[i * 2 + 1].charCodeAt(0) - 1768 + (msg[i * 2 + 1 - (i & 1)].charCodeAt(0) << 4))).join('');
}
console.log(Decode('gggruggvucftvghtrhho'));