mcrypt_decrypt function stuck in eternal loop - actionscript-3

I'm working through a decryption function and have hit a wall. I'm not very experienced with Actionscript but when I run this function it goes on an infinite loop.
private function mcrypt_decrypt(param1:Array, param2:Number,param3:Number): Array {
var _loc4_:* = -9.57401312E8;
while(_loc4_)
{
var param3:Number = param3 - ((param2 << 4 ^ param2 >>> 5) + param2 ^ _loc4_ + param1[_loc4_ >>> 11 & 3]);
_loc4_ = _loc4_ - -1640531527;
var param2:Number = param2 - ((param3 << 4 ^ param3 >>> 5) + param3 ^ _loc4_ + param1[_loc4_ & 3]);
}
return [param2,param3];
}
From testing I see that loc4 starts as -9.57401312E8 and then as iteration continues -1640531527 is removed each time, but in what case would this satisfy the while condition? Any ideas?

Your function is stuck in an eternal loop because of this:
_loc4_ = _loc4_ - -1640531527;
A double minus sign is the same as a plus sign. Since the value is never zero, the evaluation in your loop always returns true.

Related

How to correctly calculate a nonlinear function and plot its graph in Octave?

Goal: Plot the graph using a non-linear function.
Function and graph
This is my first time working at Octave. To plot the graph, I need to calculate a function in the range Fx (0.1 ... 10).
I tried to implement this by looping the function through the for loop, writing the results to an array (x-axis - Fn, y-axis - function value), then loading the arrays into the plot() function.
Fn = 1
Ln = 5
Q = 0.5
function retval = test (Fn, Ln, Q)
# Fn squared (for common used)
Fn = Fn^2
# Node A + Node B
nodeA = Fn * (Ln - 1)
nodeB = (Ln * Fn - 1)^2 + Fn * (Fn - 1)^2 * (Ln - 1)^2 * Q^2
nodeB = sqrt(nodeB)
# Result
result = nodeA / nodeB
retval = result
return;
endfunction
frequencyArray = {}
gainArray = {}
fCount = 1
gCount = 1
for i = 0:0.5:5
# F
Fn = i
frequencyArray{fCount} = Fn
fCount = fCount + 1
# G
gainArray{gCount} = test(Fn, Ln, Q)
gCount = gCount + 1
end
plot(frequencyArray, gainArray);
As a result, I get an error about the format of the arrays.
>> plot(frequencyArray, gainArray);
error: invalid value for array property "xdata"
error: __go_line__: unable to create graphics handle
error: called from
__plt__>__plt2vv__ at line 495 column 10
__plt__>__plt2__ at line 242 column 14
__plt__ at line 107 column 18
plot at line 223 column 10
In addition to the error, I believe that these tasks are solved in more correct ways, but I did not quite understand what to look for.
Questions:
Did I choose the right way to solve the problem? Are there any more elegant ways?
How can I fix this error?
Thank you!
If I have correctly interpreted what you are trying to do, the following should work. Firstly, you need to use the term-by-term versions of all arithmetic operators that act on Fn. These are the same as the normal operators except preceded by a dot. Next, you need to put Fn equal to a vector containing the x-values of all the points you wish to plot and put Q equal to a vector containing the values of Q for which you want to draw curves. Use a for-loop to loop through the values of Q and plot a single curve in each iteration of the loop. You don't need a loop to plot each curve because Octave will apply your "test" function to the whole Fn vector and return the result as a vector of the same size. To plot the curves on a log axis, use the function "semilogx(x, y)" insetad of "plot(x, y)". To make the plots appear on the same figure, rather than separate ones put "hold on" before the loop and "hold off" afterwards. You used cell arrays instead of vectors in your for-loop, which the plotting functions don't accept. Also, you don't need an explicit return statement in an Octave function.
The following code produces a set of curves that look like the ones in the figure you pasted in your question:
Ln = 5
function result = test (Fn, Ln, Q)
# Fn squared (for common used)
Fn = Fn.^2;
# Node A + Node B
nodeA = Fn .* (Ln - 1);
nodeB = (Ln .* Fn .- 1).^2 + Fn .* (Fn .- 1).^2 .* (Ln - 1)^2 * Q^2;
nodeB = sqrt(nodeB);
# Result
result = nodeA ./ nodeB;
endfunction
Fn = linspace(0.1, 10, 500);
Q = [0.1 0.2 0.5 0.8 1 2 5 8 10];
hold on
for q = Q
K = test(Fn, Ln, q);
semilogx(Fn, K);
endfor
hold off

How to pass variadic arguments in Octave

I would like to implement a function duration = timer(n, f, arguments_of_f) that would measure how much time does a method f with arguments arguments_of_f need to run n times. My attempt was the following:
function duration = timer(n, f, arguments_of_f)
duration = 0;
for i=1:n
t0 = cputime;
f(arguments_of_f);
t1 = cputime;
duration += t1 - t0;
end
In another file, I have
function y = f(x)
y = x + 1;
end
The call d1 = timer(100, #f, 3); works as expected.
In another file, I have
function y = g(x1, x2)
y = x1 + x2;
end
but the call d2 = timer(100, #g, 1, 2); gives an error about undefined
argument x2, which is, when I look back, somehow expected, since I pass only
1 to g and 2 is never used.
So, how to implement the function timer in Octave, so that the call like
timer(4, #g, x1, ... , xK) would work? How can one pack the xs together?
So, I am looking for the analogue of Pythons *args trick:
def use_f(f, *args):
f(*args)
works if we define def f(x, y): return x + y and call use_f(f, 3, 4).
You don't need to pack all the arguments together, you just need to tell Octave that there is more than one argument coming and that they are all necessary. This is very easy to do using variadic arguments.
Your original implementation is nearly spot on: the necessary change is minimal. You need to change the variable arguments_to_f to the special name varargin, which is a magical cell array containing all your arbitrary undeclared arguments, and pass it with expansion instead of directly:
function duration = timer(n, f, varargin)
duration = 0;
for i=1:n
t0 = cputime;
f(varargin{:});
t1 = cputime;
duration += t1 - t0;
end
That's it. None of the other functions need to change.

Python Function- Return Value is 'not defined'

For my encryption code, I am trying to a return a value from one function because it is used in the next. I keep getting an error, telling me that the name 'cipher_text' is not defined. Please help!
Error:
(line 7)
decryption (cipher_text, shift)
NameError: name 'cipher_text' is not defined
def main():
user_input = input ("Enter string: ")
shift = int(input ("Enter a shift that is between 1 and 26: "))
while shift<1 or shift>26:
shift = input ("ERROR: Shift must be between 1 and 26: ")
encryption (user_input, shift)
decryption (cipher_text, shift)
frequency (user_input)
def frequency(user_input):
freq_char = None
for char in user_input:
charcount = user_input.count(char)
if (charcount != 0):
freq_char = char
print (freq_char)
return fre_char
def encryption(user_input, shift):
cipher_text = ''
for char in user_input: #for every character in input
if char == ' ':
cipher = char
cipher_text += cipher
else:
cipher_num = (ord(char))+(shift)%26 #using ordinal to find the number
cipher= ''
cipher = chr(cipher_num)# using chr to convert back to a letter
cipher_text += cipher
print ("The encrypted text is:",cipher_text)
return(cipher_text)
def decryption (cipher_text, shift):
decrypt_text = ''
cipher_text = ''
for char in cipher_text: #for every character in the encrpted text
decrypt_num = (ord(char))+(int(shift))%26
decrypt= ''
decrypt = chr(decrypt_num)
decrypt_text += decrypt
print("The decrypted text is:", decrypt_text)
return(decrypt_text)
main()
Your problem is in the lines
encryption (user_input, shift)
decryption (cipher_text, shift)
as the exception tells you. If you had included the traceback with your question this would be very clear.
Variables you declare in one function are local to that function. This is a good thing! It lets you write functions like
def foo():
x = 1
return x * x
def bar():
for x in xrange(10):
print "Count: %s" % x
without them blowing each other up.
If you call a function that returns something and you want to use it, you need to use it directly, or assign it to something:
# assign
x = foo()
print x
# use directly
print "x is %s" % foo()
in your case, you can make a minimal change to assign the result of encryption to a new variable cipher_text
def main():
...
cipher_text = encryption(user_input, shift)
decryption(cipher_text, shift)
it would be equivalent (though less clear) to call this anything else
foobar = encryption(user_input, shift)
decryption(foobar, shift)
or even to avoid use of a variable altogether
decryption(encryption(user_input, shift), shift)
def main() should be def main(cipher_text)
You can also set a default value for cipeher_text:
def main(cipher_text=""):
user_input = input ("Enter string: ")
shift = int(input ("Enter a shift that is between 1 and 26: "))
while shift<1 or shift>26:
shift = input ("ERROR: Shift must be between 1 and 26: ")
encryption (user_input, shift)
decryption (cipher_text, shift)
frequency (user_input)
And then just call main() using a value example main('some value') or just empty if you defined a default value as said before.

python 3.5.1 won't return strings from functions

I'm pretty new to python, and I'm mostly just playing around to try and teach myself.
I have a function that goes more or less like this:
str1 = "a"
str2 = "b"
str3 = "c"
thing = random.randint(1,2)
def fun():
if(thing == 1):
strVal = str1 + str2
elif(thing == 2):
strVal = str2 + str3
return strVal
outp = fun
but when I try and print(outp) to the console I get an output like <function fun at 0x000001F29138AAE8> instead of "ab" or "bc" how do I get the string result of fun to print instead of whatever this is? My searches on how to get around this have only bought up documentation and examples for the print() function, which isn't really what I want.
In your code you don't invoke your function. A function is invoked as below
outp = fun()
That's why you see function fun at 0x000001F29138AAE8. Basically, Python tells you that the variable outp is a pointer to a function at the address 0x000001F29138AAE8.

Calling a function in matlab. Is it wrong like this?

I have the following class in matlab:
classdef floating_search
properties
S,M;
end
methods
function s = support(x,y)
for i=1:length(x)
if(y(i)~=1)
s = x(i);
end
end
end
end
end
Now, in the command winows, I did the following:
>> x=1:10;
>> floating_search.S = x;
>> y=trapmf(x,[1 3 5 9])
y =
Columns 1 through 7
0 0.5000 1.0000 1.0000 1.0000 0.7500 0.5000
Columns 8 through 10
0.2500 0 0
>> floating_search.M = y;
>> floating_search.support(floating_search.S, floating_search.M)
??? Reference to non-existent field 'support'.
For the last command, why did I get this error? Am I calling the function wrong? How can I pass th values floating_search.S and floating_search.M to the function and retrieve the values of S for which Y~=1?
Thanks.
You never initialize your object.
Furthermore, I think you should reconcider using your code as a non-static method:
classdef floating_search
properties
S
M
end
methods
function s = support(obj)
for i=1:length(obj.S)
if(obj.M(i)~=1)
s = obj.S(i);
end
end
end
end
end
Then execute:
x = 1:10;
y = trapmf(x,[1 3 5 9])
myInstance = floating_search()
myInstance.S = x;
myInstance.M = y;
myInstance.support()
your class is missing a constructor.
furthermore you never initialize any object.
your floating_search.S = x; statement generates a struct called floating_search:
>> whos floating_search
Name Size Bytes Class Attributes
floating_search 1x1 256 struct
Try this instead (save file as floating_search.m):
classdef floating_search
properties
S;
M;
end
methods
% constructor - place to initialize things
function obj = floating_search()
end
% you need the first input argument 'obj', since this is a value class
% see http://www.mathworks.de/de/help/matlab/matlab_oop/comparing-handle-and-value-classes.html
function s = support(obj, x, y)
for i=1:length(x)
if(y(i)~=1)
s = x(i);
end
end
end
end
end
and then run the code:
% generate your data
x = 1:10;
y = trapmf(x,[1 3 5 9]);
# initialize object
a = floating_search()
a.S = x;
a.M = y;
a.support(a.S, a.M)