ruby on rails - What is wrong with my && operator? -



ruby on rails - What is wrong with my && operator? -

i know why outputting false when input 1982. there wrong && statement? tried using !(t==r), didn't work; reason, keeps outputting false.

def no_repeats?(year) out=true t=0 while t<4 r=0 while r<4 if (year[t] == year[r]) && t != r out=false end r+=1 end t+=1 end out end

you're complicating bit more needs be.

2.2.0-preview1 :001 > load 'no_repeat.rb' => true

testing string

2.2.0-preview1 :002 > no_repeats?("1981") => false 2.2.0-preview1 :003 > no_repeats?("1983") => true

testing integer

2.2.0-preview1 :004 > no_repeats?(1981) => false 2.2.0-preview1 :005 > no_repeats?(1983) => true

and no_repeat.rb looks like

def no_repeats?(year) digits = year.to_s.split(//) digits.size == digits.uniq.size end

edit: benchmarks

using original post real 0m0.598s user 0m0.583s sys 0m0.015s using .split(//) real 0m1.322s user 0m1.321s sys 0m0.000s using .chars.to_a real 0m0.562s user 0m0.557s sys 0m0.004s

so, in efforts create more of finish answer, i've included benchmarks, each using method 400,000 times. using split(//), taking 2x performance hit. using chars.to_a instead, you'll original speeds.

def no_repeats?(year) digits = year.to_s.chars.to_a digits.size == digits.uniq.size end

ruby-on-rails ruby

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -