How to modify the Lua If Statement? - if-statement

I was trying to use the Lua if function to represent the result with a “*” in front of the value under a circumstance using an if function.
fefec C2
--------------
2 *2
3 3
When the value in Column 1 (fefec) is smaller than 3, then it is shown as * with the value entered in C2. When the value in C1 is equal or greater than 3, it is shown the actual value in C2. The code I wrote is
(function()
if [f#fefec] < 3 then
return "*[f#fefec]"
else return [f#fefec]
end
end)()
But for the first row, I got *(2.000000000000), how can I get rid of these 0s after 2?

Related

Has number unique digits?

I am currently trying to solve a problem set on codeforce where I need to check if an positive integer number has unique digits. My solutions includes a while loop and two for loops, which is quite a lot of for such an easy task.
I found a more elegant solution but I don't fully understand how the code works. I have commented it with my remarks. Could someone explain to me the second 2) and fifth 5) part?
int unique(long long int number){
/* 1) create array/list with 10 elements, the first element seen[0]
* is equal to zero */
char seen[10] = {0};
/* 2) what is the meaning of while(some random integer number)? I thought
* that the argument must be a statement that is either true or false. */
while (number) {
int digit = number % 10; // 3) get the last digit of the number
number /= 10; // 4) removes last digit of the number
/* 5) Could someone explain to me what seen[digit]++ does. And when its
* true or false? */
if (seen[digit]++)
return 0; /* not unique */
}
return 1; /* unique */
}
Of course I tried to figure out the fifth part on my own but
#include <iostream>
using namespace std;
int main(){
char seen[10] = {0};
cout << seen[7]++ << endl;
}
print outs nothing.
I'll go by parts:
2 ) The implicit conversion between a numeric type and bool returns false if the number is zero and true otherwise. You could read while(number) like while(number != 0)
5 ) This works the same way: seen[digit]++ is an expression with the same value as seen[digit] but that then increments its value (check how post-increment works). Therefore, the first time that digit is seen, seen[digit]++ has the value 0 (so the first time the condition is not met) and increments its value to 1 (so the second time the condition will be met, making the function return).
while(number) means the cycle will repeat until number is not zero. Non-zero number is equal to true
seen[digit]++ does following:
it return current value of seen[digit]. For the first time it will be zero - as no number met.
after returning current value - it increase value by one. So for the first call it will return 0 and the seen[digit] will become 1.
So for the second call it will return 1 - that mean this number already met, so it is not unique.
Q.1 what is the meaning of while(some random integer number)? I thought that the argument must be a statement that is either true or false.
=> Yes you are right while condition checks for true and false. In case of integer, 0 is treated as false and rest of the integers as true. So, whenever number become 0, while loop will break.
Q.2 Could someone explain to me what seen[digit]++ does. And when its true or false?
=> seen is declared as an array of size 10 and initialized all entries as 0. So initially every entry of array seen is zero i.e. seen[0] = 0, seen[1] = 0, seen[2] = 1... seen[9] = 0. Now when we find digit and perform seen[digit]++ it will increase value by 1 every time.
Ok so:
Every number not equal to 0 is true and equal to 0 is false. For example 1 2 and 3 are true, but 0 is false. So while (number) will iterate as long as number != 0
seen[digit]++ first returns the value, then increments itself by one after returning the value.
The condition if(number) is same as if(number != 0).
Point 2: After we have processed the last digit in the number, the value of number/10 will be 0 (as the last digit belongs to 0-9) and there we end our loop.
Point 5: The increment number will increment the value in the array and return the old value. If the value is incremented to 2, then it means that the digit is not unique and increment operation returns us 1 and the if condition is satisfied.
In C++ 0 evaluates to false and any other number evaluates to true. That "random number" is actually modified inside the loop with number /= 10. Division of integer numbers in C++ is special in the sense that it does not yield fractions so 51/10 = 5 and 5/10 = 0. At some point number equals 0 and the loop ends.
seen[digit]++ is a commonly used trick. You lookup the table seen at position digit return the current value and increment the value by 1. So if you would modify your example code like this:
#include <iostream>
using namespace std;
int main(){
int seen[10] = {0};
cout << seen[7]++ << endl;
cout << seen[7] << endl;
}
Your console output should be:
0
1
There is also ++seen[digit] which would first increment and then return the value so you would get:
1
1

Issue when Using Counter to count pairs Python

I took an online assessment where I was given the following question :
Input 1 : Integer - Length of the list
Input 2 : Integer List - Consists of different numbers
Output : Integer
Question : Find the total number of pairs possible from the list.
Example : 5, [10,23,2,10,23]
Since, 10 & 23 occurs twice, and 2 occurs only once, there are 2 pairs. So, result should be 2.
So, I did the following & I had one of the test cases failed. The test case wasn’t shown so I’m very confused as to where I went wrong. The code is :
dict=Counter(input2)
pairs=0
count=[]
for i in dict.values() :
count.append(i)
for j in count :
pairs+=j//2
return pairs
Except one test case, all the other 7 seems to satisfy. Please help me out.
You can simply divide the value of each entry of the dict that collections.Counter returns by 2:
from collections import Counter
l = [10,10,10,20,20,20,45,46,45]
print({k: v // 2 for k, v in Counter(l).items()})
This outputs:
{10: 1, 20: 1, 45: 1, 46: 0}
Or if you only want the total number of pairs:
print(sum(v // 2 for v in Counter(l).values()))
This outputs:
3

How does this program print number's digits in reverse?

#include <iostream>
int main() {
int nr;
std::cin>>nr;
while (nr > 0) {
int digit = nr % 10;
nr /= 10;
std::cout<<digit;
}
return 0;
}
Can someone please explain the workflow of this program, basically with the input "32" it outputs "23", that is good, thats my goal, my question is, why does it say "23" instead of just "2", why is the "3" being added in the end if i only said "cout digit". I get that the "3" comes from " nr /= 10", but why is it being outputed near the "2" to farm the answer "23"?
I get that the "3" comes from nr /= 10
Before the program gets to printing 3, it prints 2, which is a remainder you get after dividing 32 by 10.
The result of this division is, indeed, 3. Next loop iteration picks it up, and prints it, because 3 % 10 is 3.
The while makes two iterations. It tests if the condition is true, executes what is inside. Digit takes the value 2, nr becomes 3, it outputs 2. Then for the second iteration, nr is still bigger than 0, so digit becomes 3, nr becomes 0, it outputs 3. The condition is no longer met, so it exits the loop. (Using 23 as an input, that is)
Flow of your code
Your code works a follows (assuming you have entered the number 32, thus the variable nr is 32).
First iteration
Step 1
while(32 > 0)
result: true.
Step 2
int digit = nr % 10;
result: digit now contains the value 2 due to the remainder (%) operation.
Step 3
nr /= 10;
result: nr now contains the value 3, because 32 / 10 results in 3.2 which is a float, but since you are assigning this
number to an integer it implicit converts to the number 3.
Step 4
std::cout<<digit;
result: 2 (the variable digit is still unaffected since the remainder operation 2).
Second iteration
Step 1
while(3 > 0)
result: true (the condition in the while-loop get called again and the variable nr is 3).
Step 2
int digit = nr % 10;
result: digit now contains the value 3, because 3 % 10 = 3.
Step 3
nr /= 10;
Is not relevant anymore for any changes of the flow besides that the while-loop will terminate.
Step 4
std::cout<<digit;
result: 3 (since the digit variable is now 3).
So the complete output will be 23.
Because the while loop means the code within it is repeated until the condition is no longer satisfied. For a two digit number the print statement will therefore be executed twice.

Squeak Smalltalk getting "adaptToNumber:andSend:" error when creating linked list

I have defined list structure and now I am trying to create a list with some rules.
Here is the code so far:
List: aa
| i start current aNumber|
start := Test new setValue:2.
current := start.
aNumber:=3.
i:=0.
[i<=aa] whileTrue:[
current:=start.
[aNumber \\ current =0] whileFalse:
[[current getNext = nil] ifTrue:[current addLinkedVlue:aNumber. i=i+1].current:=current getNext].
aNumber:=aNumber+1].
I have method printListFrom which gets parameter aa. The aim is to get a list which has lenght of aa. The list can't contain numbers which can be divided without reminder by numbers which already are in list. Forexample, if list contains numbers: 2 3 5 and we need to check 6 then 6/2 = 3, it has no reminder, so we can't add it to the list. If we want to check 7, then 7/2=3 reminder 1, 7/3 = 2 reminder 1, 7/5 = 1 reminder 2, in this case we can add 7 to the list.
If we have that aa = 3, as a result I must get list which has 3 numbers(2 3 5), if aa = 4, then list should be (2 3 5 7) and so on.
In my code, in whileTrue loop I divide aNumber by number in the list (current), if I get that reminder is 0 I just add 1 to aNumber, if reminder is greater than 0 then I divide aNumber by next number in list, if I have divided aNumber by all numbers in list and got some reminder after every division I add 1 to i which represents the lenght of the list, and I add also aNumber to the list.
Code is not running well, i am getting :
MessageNotUnderstood: adaptToNumber:andSend:
and I don't know whats wrong.
Here is declaration of other methods which I have declared and used in List method:
setValue: i
a := i.
getNext
^next
addLinkedValue: n
next := Test new setValue: n.
The problem is in this line:
[aNumber \\ current] whileFalse:
because aNumber is an Integer but current isn't. In fact current is an instance of the Test class and therefore aNumber doesn't know how to deal with the message \\ when the argument (current in this case) is not an Integer.
What the receiver aNumber does to try to resolve this problem is to ask the argument to cope with the situation, and to do so it informs the argument to adaptToInteger: aNumber andSend: #quo:.
Note that the selector is not \\ but quo:. The reason is in the way \\ is implemented, which makes the receiver realize that the argument is not an Integer not before sending it the message quo:.
Now, given that you haven't implemented adaptToInteger:andSend: in Test the implementation inherited from Object enters the scene and the instance of Test receives adaptToNumber:andSend:, which is more general.
A solution would then consist in implementing this method in Test as
adaptToNumber: aNumber andSend: aSymbol
^aNumber perform: aSymbol withArgument: a
which delegates the message to the (numeric) instance variable a.
Having said this, I would recommend you to also revise the code trying to make it simpler.

'if' and 'for' loop questions

This will be my last question for the evening and a while. I have worked my way through a 100 mark Java assessment and I am now stuck on my final two points. If anyone could help me out, it would be greatly appreciated. I am tired, feeling like a grade-A nub and just want it over with!
Study the two instance methods below and then select only the options that are correct.
public char[] methodA()
{
char[] alphas = {'s', 't', 'e', 'a', 'm'};
char temp = alphas[0];
int i = 0;
while (i < alphas.length - 1)//1
{
alphas[i] = alphas[i+1]; //2
i++;
}
alphas[alphas.length-1]=temp;
return alphas;
}
public char methodB()
{
char [] alphas = {'s','a','u','s','a','g','e'};
char first = alphas[0];
for (int i= 1; i < alphas.length; i++) //3
{
if (alphas[i] < first) //4
{
first = alphas[i];
}
}
return first;
}
The assignment statement labelled //2 will put a copy of the char
element one to the right of the current element in alphas into the
current element in alphas.
The for loop header labelled //3 will be evaluated 7 times.
The if statement labelled //4 will update the value held by the
variable first if the value held in the current element in alphas
comes before the current value of first.
The boolean condition in the line labelled //1 will evaluate to
false repeatedly until i takes the value 4.
The returned value on invoking methodA is a char array containing
the values 't', 'e', 'a', 'm' and 's'.
The returned value from invoking methodB is the character 'u'.
I believe 1 to be true. Not sure why.
I think 2 is false as the for loop is evaluated 6x, not 7.
Not sure on 3 or 4.
5 I got to be true
6 I got to be false.
If anyone can help I owe them a beer, a cookie and a cuddle!!
It is true because alphas[i] = alphas[i+1] essentially will take element at position i and and replace it with the next element in the array at i + 1 (or another way to say it, its adjacent element).
I believe this is false, the keyword here is evaluated. A loop will evaluate to the stopping point, check the exiting condition, then kick out. So it will finish evaluating 1, 2, 3, 4, 5, 6, 7 <--- evaluates the value and kicks
This will be true. the expression if (alphas[i] < first) is asking if the value stored in first is greater than the value in alphas[i] or inversely ... if alphas[i] is less than first. This is essentially performing a max/min algorithm because the final number in first will be the smallest value in the alphas array. In this case, the letters will be evaluated based upon their ascii values.
This is false. The expression will evaluate to true until 4. If this were not the case then the question 1 would not be true because it would simply throw an IndexOutOfBoundsException.
This is true, because it is taking the adjacent char and placing it in the current position, then the line alphas[alphas.length-1]=temp puts the first char into the last position. More specifically, after the while your array will look like this: { t, e, a, m, m } then after the last line it will complete the set with { t, e, a, m, s }
The last one is false. Like I mentioned in question 3, it is essentially performing a min search. the character u has a greater valued ascii value than any other letter in the sequence. a is the lowest letter in the set.
Great job on making an attempt.
1 is true (a[i+1] is to the right of a[i], so a[i] = a[i+1] copies the value on the right to the current)
2 is true because the loop condition is evaluated 7 times -- 6 times as true causing the body to be executed 6 times, and then 1 time as false to break the loop.
3 sounds true, a value like 'b' is considered to "come before" a value like 'd' (comparing numeric ascii codes)
4 is true because the condition 4 < (5 - 1) is false
5 is true as you think
6 is false - should return 'a'
please skip the cuddle!