Doesn't remove duplicate elements from array - ruby-on-rails

I am trying to take input from user in an array .And want to remove duplicate elements but the result is weird .I don't have to use uniq or any other ruby method.Here is my code
digits = []
digits = gets.chomp.to_i
k= digits & digits
puts k
input - 1 2 3 4 1 2 3 <br>
Required output- 1 2 3 4<br>
Getting output 1

gets.chomp returns string "1 2 3 4 1 2 3"
Then you call to_i on that string:
"1 2 3 4 1 2 3".to_i => 1
Consequentially 1 & 1 => 1
You should do this:
digits = gets.chomp.split(' ').map(&:to_i)
k = digits & digits
puts k

Related

Count the number of contiguous subarrays with maximum element as x

Given an array of numbers, print the number of subarrays with maximum element as x. For example :
Input :
arr = [1, 2, 3, 3, 1]
x = [3,2,1,4]
output : 11,2,2,0
Subarrays for x = 1:
1
1
Subarrays for x = 2:
2
1 2
Subarrays for x = 3:
1 2 3
1 2 3 3
1 2 3 3 1
2 3
2 3 3
2 3 3 1
3
3 3
3 3 1
3
3 1
There are no subarray with maximum element as 4. So for x = 4 we have to print 0.
My first attempt was to generate all subarrays and count that.The time complexity of this approach is very bad(O(n^3))

How to count 2 columns with a range

A B C
Val 1 2
Val 2 1
Val 3 1
Item 1 Val 1 1
Item 2 Val 2 1
Item 3 Val 3 0
Item 4 Val 1 0
Consider the above sheet. In the first 3 rows I am counting how many times corresponding val# shows up in the sheet. I have done that with: =COUNTIF($B$5:$B, A1) However, I can't figure out how to make it count only if the value matches and column C doesn't have a 1 next to it on same row. Is this possible?
try COUNTIFS:
=COUNTIFS(B$5:B, A1, C$5:C, "<>"&1)
make sure C column is formatted as Number

Formula with reference to header (factor list)

I would like to do something like this (Formula to find the header index of the first non blank cell of a range in Excel?) except that I want to capture all the nonblank cells.
An application of what I am expecting would produce column "prod"
2 3 5 7 11 13 | prod |
2 1 2^1
3 1 3^1
4 2 2^2
5 1 5^1
6 1 1 2^1 3^1
7 1 7^1
8 3 2^3
9 2 3^2
10 1 1 2^1 5^1
11 1 11^1
12 2 1 2^2 3^1
13 1 13^1
14 1 1 2^1 7^1
15 1 1 3^1 5^1
16 4 2^4
I wouldn't mind a result with multiple separators ie. 6= 2^1*3^1**** , as they could be removed.
This user defined function will stitch together the header value with the range value.
In a standard public module code sheet:
Option Explicit
Function prodJoin(rng As Range, hdr As Range, _
Optional op As String = "^", _
Optional delim As String = " ")
Dim tmp As String, i As Long
For i = 1 To rng.Count
If Not IsEmpty(rng.Cells(i)) Then
tmp = tmp & delim & hdr.Cells(i).Text & op & rng.Cells(i).Text
End If
Next i
prodJoin = Mid(tmp, Len(delim) + 1)
End Function
On the worksheet as,
If you absolutely must use worksheet functions then stitch 6 conditional concatenations together.
=TRIM(IF(B2, B$1&"^"&B2&" ", TEXT(,))&
IF(C2, C$1&"^"&C2&" ", TEXT(,))&
IF(D2, D$1&"^"&D2&" ", TEXT(,))&
IF(E2, E$1&"^"&E2&" ", TEXT(,))&
IF(F2, F$1&"^"&F2&" ", TEXT(,))&
IF(G2, G$1&"^"&G2&" ", TEXT(,)))

"%" and "/" simbols difference in Ruby

I'm trying to solve the FizzBuzz game.
I need to check if a number is divisible by 3. So, when we take a number and divide it by 3, we need that operation to have NO REST, or 0 REST.
The solution given to me is this:
def fizzbuzz(number)
if number % 3 == 0
return "Fizz"
end
Why does they propose to use the "%"symbol? Why not the "/"symbol? Or both work as the same?
How should I check if the division has NO REST?
division operator / - gives the quotient of the division whatever the remainder of the division is. So you cannot determine if a number is perfectly divisible (remainder = 0) or not perfectly divisible (with non-zero remainder) using a division operator (/).
10 / 3
#=> 3
modulo operator % - gives the remainder of the division. If perfectly divisible, the output is 0, if not-perfectly divisible the output is non-zero value.
10 % 3
#=> 1
In your case number % 3 == 0 is true only if number is divisible by 3 with 0 remainder (i.e if number passed into the method frizzbuzz is a multiple of 3 like -12, -3, 3, 6, 9, etc )
Ruby has 4 division operators.
divmod returns the division and the remainder
number = 15
number.divmod(7) # => 2, 1
modulo alias % returns the remainder only
number = 15
number % 7 # => 1
number.modulo(7) # => 1
div alias / returns the integer division if both operands are integers, and floating-point division if either operand is a float.
number = 15
number / 7 # => 2
number.div(7) # => 2
1.0 * number / 7 # => 2.142857142857143
fdiv always returns a full precision floating-point division
number = 15
number.fdiv(7) # => 2.142857142857143
% is the Modulus - Divides left hand operand by right hand operand and returns remainder. \ is just the Divider. No Rest means that x % y == 0.
lets take an example to understand better:
number1 = 12
number2 = 13
lets see if number1 and number2 is devisible by 4 ok?
number1 / 4 = 3 and the rest = 0
number2 / 4 = 3 and the rest = 1
so the "/" operation let us know the result of devision operation
and the "%" operation let us know the rest of devision operation
so if we take our examples the number1 is devisible by 3 because
number1 % 3 = 0 ( the rest )

Lua with calculator script

I'm trying to create a calculator for my own use . I don't know how to make it so that when the user inputs e.g. 6 for the prompt lets the user type in 6 numbers. So if I wrote 7 , it would give me an option to write 7 numbers and then give me the answer, And if i wrote 8 it will let me write 8 numbers...
if choice == "2" then
os.execute( "cls" )
print("How many numbers?")
amountNo = io.read("*n")
if amountNo <= 2 then print("You cant have less than 2 numbers.")
elseif amountNo >= 14 then print("Can't calculate more than 14 numbers.")
elseif amountNo <= 14 and amountNo >= 2 then
amountNmb = amountNo
if amountNmb = 3 then print(" Number 1")
print("Type in the numbers seperating by commas.")
local nmb
print("The answer is..")
The io.read formats are a bit limiting.
If you want a comma-separated list to be typed, I suggested reading a whole line and then iterating through each value:
local line = io.input("*l")
local total = 0
-- capture a sequence of non-comma chars, which then might be followed by a comma
-- then repeat until there aren't any more
for item in line:gmatch("([^,]+),?") do
local value = tonumber(item)
-- will throw an error if item does not represent a number
total = total + value
end
print(total)
This doesn't limit the count of values to any particular value—even an empty list works. (It is flawed in that it allows the line to end with a comma, which is ignored.)
From what I understand, you want the following:
print "How many numbers?"
amountNo = io.read "*n"
if amountNo <= 2 then
print "You can't have less than 2 numbers."
elseif amountNo >= 14 then
print "Can't calculate more than 14 numbers."
else
local sum = 0
for i = 1, amountNo do
print( ('Enter number %s'):format(i) )
local nmb = io.read '*n'
sum = sum + nmb
end
print( ('The sum is: %s'):format(sum) )
end
If the user separates the numbers with commas, they don't need to state how many they want to add, you can just get them all with gmatch. Also with the right pattern you can ensure you only get numbers:
local line = io.input()
local total = 0
-- match any number of digits, skipping any non-digits
for item in line:gmatch("(%d+)") do
total = total + item
end
With input '4 , 6 , 1, 9,10, 34' (no quotes), then print(total) gives 64, the correct answer

Resources