Nim lang syntax a = b = c = 7 - nim-lang

How to solve error on syntax var a = b = c = 7 being one RHS value for many LHS just like in C:
int a,b,c ;
a = b = c = 7 ;
so on Nim lang:
Error: invalid indentation
Help out, thanks in advance

This is one of the earliest things in the Tutorial:
var a, b, c = 7

xbello answered the question, but if we want to be similar at the C code, we should also answer how to do multiple affectations after declaration :
var a,b,c
(a,b,c) = (7,7,7)

Related

How do i convert all the variables into an int straightaway

a,b,c = input(a,b,c).split()
would need a way to immediately convert a,b,c into int without having to do something like
a = int(a)
b = int(b)
c = int(c)
please help :)
l = input().split(',')
l = [ int(i) for i in l ]
a, b, c = *l
Run the code and enter 3 integers separated by ',' then a b and c will contain the 3 integers

Converting strings to code

This is a programming question for Python 3.5
Say I have a string s which I define as
s = "a + b"
and I have some variables
a = 1
b = 2
How can I make a function from the string that uses the variables a and b?
s is something that you can arbitrarily enter as a string input.
def f1(s):
???Code???
return a + b
or
s = "a*b"
def f2(s):
???Code???
return a*b
Does this involve symbolic programming? Is this even possible?
You can use the eval function.
https://docs.python.org/3/library/functions.html#eval
Example:
a = 2
b = 5
eval('a+b') # 7

ACSL Bit String Flicking

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

Equivalent of adding strings to a loop, for strings (matlab)?

How would I be able to do the equivalent of this with strings:
a = [1 2 3; 4 5 6];
c = [];
for i=1:5
b = a(1,:)+i;
c = [c;b];
end
c =
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
Basically looking to combine several strings into a Matrix.
You're growing a variable in a loop, which is a kind of sin in Matlab :) So I'm going to show you some better ways of doing array concatenation.
There's cell strings:
>> C = {
'In a cell string, it'
'doesn''t matter'
'if the strings'
'are not of equal lenght'};
>> C{2}
ans =
doesn't matter
Which you could use in a loop like so:
% NOTE: always pre-allocate everything before a loop
C = cell(5,1);
for ii = 1:5
% assign some random characters
C{ii} = char( '0'+round(rand(1+round(rand*10),1)*('z'-'0')) );
end
There's ordinary arrays, which have as a drawback that you have to know the size of all your strings beforehand:
a = [...
'testy' % works
'droop'
];
b = [...
'testing' % ERROR: CAT arguments dimensions
'if this works too' % are not consistent.
];
for these cases, use char:
>> b = char(...
'testing',...
'if this works too'...
);
b =
'testing '
'if this works too'
Note how char pads the first string with spaces to fit the length of the second string. Now again: don't use this in a loop, unless you've pre-allocated the array, or if there really is no other way to go.
Type help strfun on the Matlab command prompt to get an overview of all string-related functions available in Matlab.
You mean storing a string on each matrix position? You can't do that, since matrices are defined over basic types. You can have a CHAR on each position:
>> a = 'bla';
>> b = [a; a]
b <2x3 char> =
bla
bla
>> b(2,3) = 'e'
b =
bla
ble
If you want to store matrices, use a cell array (MATLAB reference, Blog of Loren Shure), which are kind of similar but using "{}" instead of "()":
>> c = {a; a}
c =
'bla'
'bla'
>> c{2}
ans =
bla

MATLAB generate combination from a string

I've a string like this "FBECGHD" and i need to use MATLAB and generate all the required possible permutations? In there a specific MATLAB function that does this task or should I define a custom MATLAB function that perform this task?
Use the perms function. A string in matlab is a list of characters, so it will permute them:
A = 'FBECGHD';
perms(A)
You can also store the output (e.g. P = perms(A)), and, if A is an N-character string, P is a N!-by-N array, where each row corresponds to a permutation.
If you are interested in unique permutations, you can use:
unique(perms(A), 'rows')
to remove duplicates (otherwise something like 'ABB' would give 6 results, instead of the 3 that you might expect).
As Richante answered, P = perms(A) is very handy for this. You may also notice that P is of type char and it's not convenient to subset/select individual permutation. Below worked for me:
str = 'FBECGHD';
A = perms(str);
B = cellstr(reshape(A,7,[])');
C = unique(B);
It also appears that unique(A, 'rows') is not removing duplicate values:
>> A=[11, 11];
>> unique(A, 'rows')
ans =
11 11
However, unique(A) would:
>> unique(A)
ans =
11
I am not a matlab pro by any means and I didn't investigate this exhaustively but at least in some cases it appears that reshape is not what you want. Notice that below gives 999 and 191 as permutations of 199 which isn't true. The reshape function as written appears to operate "column-wise" on A:
>> str = '199';
A = perms(str);
B = cellstr(reshape(A,3,[])');
C = unique(B);
>> C
C =
'191'
'199'
'911'
'919'
'999'
Below does not produce 999 or 191:
B = {};
index = 1;
while true
try
substring = A(index,:);
B{index}=substring;
index = index + 1;
catch
break
end
end
C = unique(B)
C =
'199' '919' '991'

Resources