I need help with an ACSL Problem. The contest was done in 2014-2015. It is just practice and I want to see if I did the problem correctly.
Bit-String Flicking:
Solve for x (5 bits) in the following equation. How many unique solutions are there?
(RCIRC-2(LSHIFT-1 (NOT X)))=00101
After solving I got 16 unique solutions although I can't find the answer anywhere and need the help of you smart and creative people!
Thanks
Here's the idea to solve this problem:
transfer RCIRC, LSHIFT and NOT to the other side like this:
RCIRC-2(LSHIFT-1(NOT X))=00101 -> LSHIFT-1(NOT X) = LCIRC-2(00101)
LSHIFT-1(NOT X) = LCIRC-2(00101) -> NOT X = RSHIFT-1(LCIRC-2(00101))
NOT X = RSHIFT-1(LCIRC-2(00101)) -> X = NOT(RSHIFT-1(LCIRC-2(00101)))
X = NOT(RSHIFT-1(LCIRC-2(00101)))
solve it:
X = NOT(RSHIFT-1(LCIRC-2(00101)))
X = NOT(RSHIFT-1(10100))
X = NOT(01010)
X = 10101
And that would be it. The point is that when you transfer from one side to another you convert left to right and right to left.
So it's only one correct solution!
Represent each bit as letters A-E
(RCIRC-2(LSHIFT-1(NOT ABCDE))) = 00101
(RCIRC-2(LSHIFT-1(abcde))) = 00101
(RCIRC-2(bcde0)) = 00101
e0bcd = 00101
E0BCD = 10010
B = 0, C = 1, D = 0, E = 1
X = *0101
Related
a = "3030104AF43B000A3F1D200619D09FE00403031324354650004FFFFF"
b = "3030104BE3B000C3DF1D200617183BA00403030335F5B6F0004FFFFF"
Let's say a and b are two hexadecimal string values of which are equal and even in length. They also share the same format such that differences occurring between both strings always happen at the same position (But, initially I do not know which position these differences occur). For example a's first six digits are the same as b's first six digits. i.e., 303010. The following 4 digits are different 4AF43B in a compared with b, after which the next two digit are the same for a and b (00). This pattern follows on until the end of both strings.
I have written code to store the differences occurring as different elements in a list.
seed = "3030104AF43B000A3F1D200619D09FE00403031324354650004FFFFF"
seed2 = "3030104BE3B000C3DF1D200617183BA00403030335F5B6F0004FFFFF"
seed = seed.rstrip("FF")
seed2 = seed2.rstrip("FF")
differences_list1 = []
differences_list2 = []
sequence1 = ""
sequence2 = ""
for pair in range(int(len(seed) / 2)):
data_pair1 = seed[pair * 2:(pair * 2) + 2]
data_pair2 = seed2[pair * 2:(pair * 2) + 2]
if data_pair1 == data_pair2:
if sequence1 == "" and sequence2 == "":
continue
# here, we know it is not an empty sequence
differences_list1.append(sequence1)
differences_list2.append(sequence2)
sequence1 = ""
sequence2 = ""
continue
# when they are not equal to each other
sequence1 = sequence1 + data_pair1
sequence2 = sequence2 + data_pair2
print(str(differences_list1))
print(str(differences_list2))
Output (which I want):
['4AF43B', '0A3F', '19D09FE0', '1324354650']
['4BE3B0', 'C3DF', '17183BA0', '0335F5B6F0']
I've gotten the output as I desired somehow but I would like to know how can I improve/write my code in a more pythonic way (specifically python3.9)?
I need to do a progam in blockly code for translate numbers from arabigos to roman up to 4000,but I don't know what I am doing wrong .
I only can use functions ,variables ,maths and logic (attached html code ).
who can help me with this please ,I'll be thankful ;))
https://blockly-demo.appspot.com/static/demos/code/index.html#zq536j
Let me see if I can think of something :)
Perhaps an example can help me: n = 1234 I can start by dividing by 1000 and take the integer part:
M = Math.floor(n/1000)
now M is 1 Now I can remove 1000*M from n and continue:
n = n-1000*M -> so now n is only 234.
After that:
D = Math.floor(n/500)
n = n-500*D
so D is 0 and n is still 234, because 234 does not contain any 500-eds.
And so no:
C = Math.floor(n/100)
n = n-100*C
which gives that C is 2 and n is 34.
And so on:
L = Math.floor(n/50)
n = n-50*L
which gives that L is 0 and n is 34.
Then:
X = Math.floor(n/10)
n = n-10*X
which gives that X is 3 and n is 4.
And finally
I = n
So now:
M=1
D=0
C=2
L=0
X=3
I=4
so you just have to make a clever enough function that prints it like:
"M CC XXX IV"
and you are done ;)
PS I hope this was not homework :D
I'm really tired, so I'm probably just forgetting something, but here it is. It's been a long time since I've even touched quadratics. The answers are totally off, for example:
2x**2 – 4x – 3 = 0 should result in x = –0.58, x = 2.58. Instead, I get x=10, x=-2. I'm sure I'm doing something super basic wrong here.
import math
import sys
a = float(input('a ='))
b = float(input('b ='))
c = float(input('c ='))
cont = float(input('Continue? (1/0)'))
a1 = int((-b + math.sqrt(b**(2)-4*a*c))/2*a)
a2 = int((-b - math.sqrt(b**(2)-4*a*c))/2*a)
if cont == 1:
print(a)
print(b)
print(c)
print(cont)
print('x1 =',a1)
print('x2 =', a2)
elif cont == 0:
sys.exit
Add parenthesis to your equation around (2*a):
a1 = (-b + math.sqrt(b**(2)-4*a*c))/(2*a)
a2 = (-b - math.sqrt(b**(2)-4*a*c))/(2*a)
Also, do you mean to convert to integer?
I can easily replace each every letter by doing them one after another but when i use loop it won't.
a = strrep(a,'b','z');
a = strrep(a,'a','e');
a = strrep(a,'c','f');
but for i = 1:size(a,2)
a = strrep(a,'b','z');
a = strrep(a,'a','e');
a = strrep(a,'c','f');
end
only change 'b' and 'a' not all. so lets say if we str 'abcdabc' then replace one after another gives right answer but loop shows the result as 'ezedeze'. so please help with this.
Here are two approaches:
With changem (from the Mapping Toolbox):
str = 'basic example string';
old = 'abcde';
new = 'fhdot';
str = changem(str, new, old);
With ismember:
str = 'basic example string';
old = 'abcde';
new = 'fhdot';
[ind1, ind2] = ismember(str, old);
new = 'fhdot';
str(ind1) = new(ind2(ind1));
I have 2 cell of strings and I would like to order them according to the first one.
A = {'a';'b';'c'}
B = {'b';'a';'c'}
idx = [2,1,3] % TO FIND
B=B(idx);
I would like to find a way to find idx...
Use the second output of ismember. ismember tells you whether or not values in the first set are anywhere in the second set. The second output tells you where these values are located if we find anything. As such:
A = {'a';'b';'c'}
B = {'b';'a';'c'}
[~,idx] = ismember(A, B);
Note that there is a minor typo when you declared your cell arrays. You have a colon in between b and c for A and a and c for B. I placed a semi-colon there for both for correctness.
Therefore, we get:
idx =
2
1
3
Benchmarking
We have three very good algorithms here. As such, let's see how this performs by doing a benchmarking test. What I'm going to do is generate a 10000 x 1 random character array of lower case letters. This will then be encapsulated into a 10000 x 1 cell array, where each cell is a single character array. I construct A this way, and B is a random permutation of the elements in A. This is the code that I wrote to do this for us:
letters = char(97 + (0:25));
rng(123); %// Set seed for reproducibility
ind = randi(26, [10000, 1]);
lettersMat = letters(ind);
A = mat2cell(lettersMat, ones(10000,1), 1);
B = A(randperm(10000));
Now... here comes the testing code:
clear all;
close all;
letters = char(97 + (0:25));
rng(123); %// Set seed for reproducibility
ind = randi(26, [10000, 1]);
lettersMat = letters(ind);
A = mat2cell(lettersMat, 1, ones(10000,1));
B = A(randperm(10000));
tic;
[~,idx] = ismember(A,B);
t = toc;
fprintf('ismember: %f\n', t);
clear idx; %// Make sure test is unbiased
tic;
[~,idx] = max(bsxfun(#eq,char(A),char(B)'));
t = toc;
fprintf('bsxfun: %f\n', t);
clear idx; %// Make sure test is unbiased
tic;
[~, indA] = sort(A);
[~, indB] = sort(B);
idx = indB(indA);
t = toc;
fprintf('sort: %f\n', t);
This is what I get for timing:
ismember: 0.058947
bsxfun: 0.110809
sort: 0.006054
Luis Mendo's approach is the fastest, followed by ismember, and then finally bsxfun. For code compactness, ismember is preferred but for performance, sort is better. Personally, I think bsxfun should win because it's such a nice function to use ;).
This seems to be significantly faster than using ismember (although admittedly less clear than #rayryeng's answer). With thanks to #Divakar for his correction on this answer.
[~, indA] = sort(A);
[~, indB] = sort(B);
idx = indA(indB);
I had to jump in as it seems runtime performance could be a criteria here :)
Assuming that you are dealing with scalar strings(one character in each cell), here's my take that works even when you have not-commmon elements between A and B and uses the very powerful bsxfun and as such I am really hoping this would be runtime-efficient -
[v,idx] = max(bsxfun(#eq,char(A),char(B)'));
idx = v.*idx
Example -
A =
'a' 'b' 'c' 'd'
B =
'b' 'a' 'c' 'e'
idx =
2 1 3 0
For a specific case when you have no not-common elements between A and B, it becomes a one-liner -
[~,idx] = max(bsxfun(#eq,char(A),char(B)'))
Example -
A =
'a' 'b' 'c'
B =
'b' 'a' 'c'
idx =
2 1 3