Matematica If Statement - wolfram-mathematica

In Mathematica i am using this following code:
If[ x [[1]] <= 4 && x [[2]] <= 4, "True","False"]
True
This is where i get confused:
If[ True, count=count + 1, count=count]
I do not no how to access the answer from the line before code

Why don't just
If[ x [[1]] <= 4 && x [[2]] <= 4, count++]
?

In addition to belisarius' answer you may consider:
count += Boole[x[[1]] <= 4 && x[[2]] <= 4]
Also, you said: "I do not no how to access the answer from the line before code."
You could use %:
If[ x [[1]] <= 4 && x [[2]] <= 4, True, False];
If[%, count=count + 1, count=count]

Literally you could just fit one piece of code inside the other:
If[
If[ x [[1]] <= 4 && x [[2]] <= 4, True, False],
count=count + 1, count=count
]
Note that the strings "True" and "False" have been replaced with their logical values True and False

Related

Check if a variable equal X or Y

I'm stuck with something very basic in Ruby. Maybe I did not understand how the operator || works?
a_to_i = [a = 12, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0]
puts a
puts a == (3 || 12)
I'm waiting the output : true
But it returns false instead.
Do you see where am i doing wrong ? Thank you very much in advance.
3 || 12 returns 3, as it is a truthy value.
You need to ask two questions here:
puts (a == 3 || a == 12)
Alternatively, if the list of options is slightly longer:
puts [3, 12].include? a
The result of the statment (3 || 12) is 3, always resulting in a == 3. You'll need to check both values separately.
puts((a == 3) || (a == 12))

Using `map` to solve 'FizzBuzz'

I tired to use map to solve fizzbuzz.
def fizzbuzz(n)
array =(1..n).to_a
array.map{|x|
if x % 3 == 0 && x % 5 == 0
x = 'FizzBuzz'
elsif x % 3 == 0
x = 'Fizz'
elsif x % 5 == 0
x = 'Buzz'
end
}
array
end
Somehow, it doesn't work. Do you know what's wrong?
Method map does not change the original array. Use the bang version map! instead.
Using map! as suggested by #tmc and some other changes try:
def fizzbuzz(n)
array =(1..n).to_a
array.map!{|x|
if x % 3 == 0 && x % 5 == 0
x = 'FizzBuzz'
elsif x % 3 == 0
x = 'Fizz'
elsif x % 5 == 0
x = 'Buzz'
else
x = x
end
}
p array
end
fizzbuzz(10) #=> [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz"]
As you can see I've added a call to the method fizzbuzz with an argument of 10 which you can change. And I've used p to inspect the array as well as a final else statement.

Get key from hash if value in it zero

I have 3 array region ,min,max.If min and corresponding max is both 0 then I want to get the corresponding region into an array.
region=["A","B","C","D","E","F","G"]
min= ["0","0","0","200","400","0","0"]
max= ["0","0","0","1000","1000","0","0"]
Expected output :
output=["A","B","C","F","G"]
In the above..ABCFG has corresponding min and max both 0..so I want those region valuse..i.e regions which has both min and max 0
I tried below but no able to proceed furthur
Hash[region.zip([min.map(&:to_i),max.map(&:to_i)].transpose)]
//output
{"A"=>[0, 0], "B"=>[0, 0], "C"=>[0, 0], "D"=>[200, 1000], "E"=>[400, 1000], "F"=>[0, 0], "H"=>[0, 0]}
This would do:
region.zip(min, max).select {|_, a, b| a == '0' && b == '0'}.map(&:first)
Try this:
output = []
region.each_with_index { |r, i| output << r if min[i] == '0' && max[i] == '0' }
output # => ["A","B","C","F","G"]
Another way:
emin, emax = min.to_enum, max.to_enum
region.select do
xmin = emin.next == '0'
xmax = emax.next == '0'
xmin && xmax
end
# => ["A", "B", "C", "F", "G"]
For extra credit: can this be simplified to:
region.select do
emin.next == '0' && emax.next == '0'
end
?

Project Euler #6 in Ruby (What's the difference?)

I solved #6 for Project Euler using two different methods, but the second one just happened to be coincidental. I don't really understand why I don't have to return the square of new_sum like I did for my first method. Does anyone know what's the major difference between the two?
def square_difference(num)
new_sum = 0
sum = 0
while num >= 1
sum += num**2 && new_sum += num
num -= 1
end
return new_sum**2 - sum
end
AND
def square_difference(num)
new_sum = 0
sum = 0
while num >= 1
new_sum += num && sum += num**2
num -= 1
end
return new_sum - sum
end
The precedence of && is higher than that of +=. So these expressions:
sum += num**2 && new_sum += num
new_sum += num && sum += num**2
don't work as you expected. The first one just happen to provide the seemingly "correct" result.
Divide them into separate lines and you'll see the difference. Or, at least use and instead of &&. and has a lower precedence than that of +=.
Both formulas have the same subtle bug. I stepped through it a couple of times to understand what was going on.
From the second one:
[30] pry(main)> square_difference(4)
new_sum: 16, sum: 16, num: 3
new_sum: 41, sum: 25, num: 2
new_sum: 70, sum: 29, num: 1
new_sum: 100, sum: 30, num: 0
=> 70
We can see that new_sum does not seem to be behaving as intended.
What is actually happening is that new_sum += num && sum += num**2 is being evaluated to new_sum += (num && sum += num **2), which in turn evaluates to new_sum += (sum += num **2)
This is a result of the && operator, which has higher precedence (as Yu Hao pointed out) and returns the first value which determines whether the AND condition is satisfied.
[31] pry(main)> 2 && 2
=> 2
[32] pry(main)> 2 && 4
=> 4
[33] pry(main)> 4 && 2
=> 2
[34] pry(main)> nil && 2
=> nil
[35] pry(main)> 2 && nil
=> nil

Ruby: Reducing Repetition

I'm a beginner and wrote a script for the following question below in ruby. I read that repetition isn't recommended and would like to reduce the repetition of if, elsif, else statements but can't seem to find a way.
Old-school Roman numerals. In the early days of Roman numer- als, the Romans didn’t bother with any of this new-fangled sub- traction “IX” nonsense. No sir, it was straight addition, biggest to littlest—so 9 was written “VIIII,” and so on. Write a method that when passed an integer between 1 and 3000 (or so) returns a string containing the proper old-school Roman numeral. In other words, old_roman_numeral 4 should return 'IIII'. Make sure to test your method on a bunch of different numbers. Hint: Use the inte- ger division and modulus methods on page 37.
For reference, these are the values of the letters used:
I =1 V=5 X=10 L=50 C=100 D=500 M=1000
Here is my script...
puts "What is your number?"
n = gets.chomp
num = n.to_i
number = ""
l = n.length
i = 0
while true
if num > 3000
puts "Enter another number."
elsif l == 0
break
else
if l == 4
number += "M" * n[i].to_i
l -= 1
i += 1
elsif l == 3
if 1 <= n[i].to_i && n[i].to_i <= 4
number += "C" * n[i].to_i
elsif n[i].to_i == 5
number += "D"
elsif 6 <= n[i].to_i && n[i].to_i <= 9
number += "D" + "C" * (n[i].to_i - 5)
end
l -= 1
i += 1
elsif l == 2
if 1 <= n[i].to_i && n[i].to_i <= 4
number += "X" * n[i].to_i
elsif n[i].to_i == 5
number += "L"
elsif 6 <= n[i].to_i && n[i].to_i <= 9
number += "L" + "X" * (n[i].to_i - 5)
end
l -= 1
i += 1
else
if 1 <= n[i].to_i && n[i].to_i <= 4
number += "I" * n[i].to_i
elsif n[i].to_i == 5
number += "V"
elsif 6 <= n[i].to_i && n[i].to_i <= 9
number += "V" + "I" * (n[i].to_i - 5)
end
l -= 1
i += 1
end
end
end
This doesn't use integer division or modulus, but it might be instructive.
puts "What is your number?"
input = gets.to_i
numerals = {
1000 => "M",
500 => "D",
100 => "C",
50 => "L",
10 => "X",
5 => "V",
1 => "I"
}
digits = []
numerals.each do |n, digit|
while input >= n
digits << digit
input = input - n
end
end
puts digits.join
Another way, that builds a string, as #sawa suggested, rather than constructing an array and then using join:
numerals = {
1000 => "M",
500 => "D",
100 => "C",
50 => "L",
10 => "X",
5 => "V",
1 => "I"
}
input = 9658
numerals.each_with_object('') do |(n, digit),str|
nbr, input = input.divmod(n)
str << digit*nbr
end
#=> "MMMMMMMMMDCLVIII"

Resources