Ȼ/08

Top / Ȼ / 08

βˤĤ

Fizz Buzz

ñϫ⤤褦ʤΤ Fizz Buzz ˤĤƤ⥵ץȤκ򼨤Ƥ

ι¤

ޤϤĤΤ褦ι¤Ǥ롥

  1. include Math
  2.  
  3. # Fizz Buzz ֤ؿ
  4. def fb(n)
  5.   # Ϥ줫顥Ȥꤢ֥ץबư褦ˡ n ϤƤ
  6.   return n
  7. end
  8.  
  9. # ʲץ
  10.  
  11. # Ѵ줿򤤤Ŀ뤫
  12. n_upper = ARGV[0].to_i
  13.  
  14. # Ѵ줿ο
  15. n = 0
  16.  
  17. # Fizz Buzz ƹԤǽ 1 .
  18. i = 1
  19.  
  20. # n ꤵ줿ˤʤޤ i 䤷Ƥ
  21. while (n < n_upper) do
  22.   # ̽
  23.   print(fb(i),"\n")
  24.  
  25.   # Fizz Buzz νϤñʤǤʤ = Ѵ줿פȤƥȤ䤹
  26.   if (fb(i) != i) then
  27.     n += 1
  28.   end
  29.  
  30.   # ο
  31.   i += 1
  32. end

Fizz Buzz ؿμ

Fizz Buzz 餷ơоݤȤʤ n ФƥץΤ٤Ȥ(ľˤ)

  1. ޤ 15 ܿå (̤ Yes = Fizz Buzz or No)
  2. Υåη̤ No ʤС(㤨) 3 ܿå (̤ Yes = Fizz or No)
  3. Υåη̤ No ʤС(㤨) 5 ܿå (̤ Yes = Buzz or No)
  4. Υåη̤ No ʤС n ΤΤ

Ȥˤʤ롥ϴñʻְ㤨Ѥ

Fizz Buzz Ǵְ㤨ϻǽϤˤĤȿʤɬפ

ơäᤷ Fizz Buzz ؿμ򸫤Ƥߤ褦
Υȡ꡼ľ˼ʤ㤨мΤ褦ˤʤ

def fb(n)
  if (n % 15 == 0) then
      return "Fizz Buzz"
    elsif (n % 3 == 0) then
      return "Fizz"
    elsif (n % 5 == 0) then
      return "Buzz"
    else 
      return n
  end
end

¤Ȥʤɲ̵餤ñelsif ʤɻȤʤƤ Τ褦 if ʸľ˽ŤͤƤäƤɤˤƤ狼äƤФʬΥץϤޤä񤷤ʤ

def fb(n)
  if (n % 15 == 0) then
    return "Fizz Buzz"
  else 
    if (n % 3 == 0) then
      return "Fizz"
    else 
      if (n % 5 == 0) then
        return "Buzz"
      else
        return n
      end
    end
  end
end

ʤä or 񤷤äȻפäͤϡ

ץߥ󥰤ˡ򤭤Ƥ뤫

ȼʬιư򿶤֤äƤߤ褦⤷פդʤСܡɤ˿ƤȤ褦ˤƤߤ褦


Collatz

ޤץ˲ݤ줿ƷǤ褦

  • k ФơCollatz δؿ f 򲿲ŬѤ 1 ˤʤ뤫Ϥؿ num_col(k) def ȤäƤ롥
  • ץ൯ưˤ礭ʿ n (100餤) Ϳ롥
  • 1 Ϥơn ޤ num_col η̤Ϥ롥

ȤȤä
warning.png num_col ؿʤмǤ롥
äȤȸȡž夲ˤĤƤ

ݤ줿ʤХݡȤȤƼʤ

ΤǡŬ˽ФФȤäƤñ̤ĤʤòȤˤʤ롥

ι¤

ơι¤ñ˰ʲΤ褦ˤʤ

  1. include Math
  2.  
  3. # Collatz ؿ
  4. def col(n)
  5.   # ϸǡ
  6.   return n
  7. end
  8.  
  9. # Collatz ؿ򲿲ŬѤ 1 ˤʤ뤫֤ؿ. δؿȻؼƤ롥
  10. def num_col(k)
  11.   # ϸ
  12.   return k
  13. end
  14.  
  15. # ʲprogram
  16.  
  17. # ɤޤĴ٤뤫
  18. n_upper = ARGV[0].to_i
  19.  
  20. # 1 ˡn_upper ޤ num_col η̤Ϥ
  21. for i in 1..n_upper do
  22.   print(i, ", ",num_col(i),"\n")
  23. end


ƴؿμ

ޤ̤ǺѤ Collatz ؿ褦Τ褦ˤʤ

def col(n)
  if (n % 2 == 0) then
      return (n/2)
    else
      return (3*n)+1
  end
end

ơCollatz ؿ򲿲ŬѤ 1 ˤʤ뤫֤ؿ while Ȥä㤨мΤ褦ˤʤ

def num_col(k)
  i = 0
  n = k

  while (n > 1) do # ޤ 1 ˤʤäƤʤ Collatz ؿŬѤ.
    n = col(n)
    i += 1
  end

  # Τϡwhile ȴʤΤǡn=1 ˤʤäƤ롥
  return i
end

ȤϡΥץ 100 餤ޤươ̤򥰥դˤƥդȥץξݡȤȤФɤäȤñä

ץư˥ǡ㤦 : gets ̿

ץưˡܡɤե뤫ǡ㤤Ȥ奨󤬤롥
˻Ȥ̿Ȥ gets (get string ά) Ȥ̿᤬ΤǤؤܤ

ȤϡäѤˤäưʲ̤

1Ԥ1ĤΥǡ㤦

ǡϤƤ餦ӤˡΥåȤԤɤ
a ϥǡѿ̾ΡפǤ롥

a = gets
a.chomp!

⤷褦

ǽ a=gets ǥܡɤʬʸϤ졤ѿ a
Τޤޤ a κǸˡֲԥ \nפȤüʸäޤޤǤꡤ̾ϺʤΤǡ a.chomp! Ǥüʸ \n õƤ롥

ϥǡˤϤĤ⤹ .chomp! Ƥ

warning.png gets = get string Ǥ뤳Ȥ狼褦ˡϥǡϡʸפȤư롥ȤưȤѴɬפ

notes.png ĤäƤʿѤñʥץȤ⤦Ϥ¥ɽ⤹褦ˤ褦


ξ㤨мΤ褦ˤʤ(ͤʷ֤ФƤƤƤޤɤʤץǤ)

  1. include Math
  2.  
  3. print("What is x? : ") # Ϥ¥ɽ
  4. x = gets
  5. x.chomp!
  6.  
  7. print("What is y? : ") # Ϥ¥ɽ
  8. y = gets
  9. y.chomp!
  10.  
  11. z = (x.to_f + y.to_f) / 2.0
  12. # x, y .to_f ʤɤĤʤȿˤʤʤȤա
  13.  
  14. print("Average = ",z,"\n")

㤨ФΥץ¹Ԥȡ

What is x? : 1

What is y? : 2

Average = 1.5

ʤɤȤʤ롥

[] 1Ԥ1ĤΥǡ㤦. ԤϤ뤫狼ʤ

㤨С֤ĤΡץǡʿѤΤꤿǡͭ뤫ʬʤ褦ʾ礬롥礰餤Ϥ˽
ˤ while ȤäƼΤ褦ˤΤѥ

while (a = gets) do
    a.chomp!
    Ȥɤɤ󵭽
    
    ޤǤ顤⤦پäơϤСwhile μιԤ
    ϤʤСײ end ʹߤ˰ܤ
end

warning.png (Ը) 狼Ѥ˲⤹ȡϤʤ gets η̤äݤˤʤꡤa = get ˡּԡפ롥Τᡤwhile ʸξ郎Ωˡend ʲ椬ήȤȤߤ

ϼ򸫤ʤȥԥȤʤͤ⤤
notes.png ʣϤƤäƤʿѤñʥץȤ⤦Ϥ뤫ϻˤʬʤȤ褦


ξ㤨мΤ褦ˤʤ

  1. include Math
  2.  
  3. # ǡθĿ
  4. n = 0
  5.  
  6. # ǡι
  7. sum = 0.0
  8.  
  9. print("? (to stop, push Ctrl+d): ") # Ϥ¥ɽ.ǽΰʬ;פɬס
  10. while (x = gets) do
  11.   x.chomp!
  12.  
  13.   sum += x.to_f
  14.   n += 1
  15.  
  16.   print("? (to stop, push Ctrl+d): ") # ()Ϥ¥ɽ
  17. end
  18.  
  19. average = sum/n
  20.  
  21. print("\nAverage = ",average,"\n")

㤨ФΥץ¹Ԥȡ

? (to stop, push Ctrl+d): 3

? (to stop, push Ctrl+d): 2.1

? (to stop, push Ctrl+d): 4.3

? (to stop, push Ctrl+d):

Average = 3.13333333333333

(4ܤϻˡCtrl d Ʊ˲Ƥ)
ʤɤȤʤ롥

warning.png ԥ塼ǤϡϤϽפȱȤ Ctrl d Ʊ˲«ˤʤäƤ롥ФƤȤ褤Ȥ뤫⤷ʤ.

1ԤʣΥǡ㤦

1ԤʣΥǡ񤤤ƤäϤȤϡǡʬ䤷ǤˤƤޤΤޤȤפ
ǡǶڤϤ硤ŪˤϼΤ褦ˤʤ롥

a = gets
a.chomp!
ary = a.split()


ξϡѿ ary ʣΥǡǼȤʤ롥
褯狼ʤȤͤϡ̵򤻤Ρ֥ѥפŵƤޤäƤɤ

warning.png (Ը) .split() ϡ() ˻ꤷΤǶڤƤȤʸʬ䤷֤Ǥ롥⤤ʤȶڤˤʤ롥¾ξˤĤƤϳƼĴ٤补

ˡϥǡ¤ΤĤȤϡΰԤäѴɤ

ary.map!{|e| e.to_f }


褯狼ʤȤͤϡ̵򤻤Ρ֥ѥפŵƤޤäƤɤ

warning.png (Ը) .map! Ȥ줾Ʊä̿Ǥ롥

notes.png ơεǽȤäơ1ԤʣϤƤäƤʿѤñʥץȤ⤦Ϥ뤫ϻˤʬʤȤ褦 ޤѿѴɤʤäƤΤ򤹤뤿ˡηаޤɽ⤹褦ˤ褦.


ξ㤨мΤ褦ˤʤ

  1. include Math
  2.  
  3. # Ϥ¥ɽ.
  4. print("? : ") 
  5.  
  6. # ޤ̤礭ʸĤȤ
  7. x = gets
  8. x.chomp!
  9.  
  10. #  x ɽƤߤ
  11. p x
  12.  
  13. # ǶڤäѴ
  14. data = x.split()
  15.  
  16. # ɽƤߤ
  17. p data
  18.  
  19. # Ǥ٤Ƥ¿Ѵ
  20. data.map!{ |e| e.to_f }
  21.  
  22. # ɽƤߤ
  23. p data
  24.  
  25. # ȤʿѤ׻
  26. sum = 0.0
  27. for i in 1..data.size do
  28.   sum += data[i-1]
  29. end
  30. average = sum/data.size
  31.  
  32. # ʿѤɽ
  33. print("Average = ",average,"\n")

㤨ФΥץ¹Ԥȡ

? : 3 2.1 4.3

"3 2.1 4.3"

["3", "2.1", "4.3"]

[3.0, 2.1, 4.3]

Average = 3.13333333333333

ʤɤȤʤ("3 2.1 4.3" Ϥ)

ܡɤǤϤʤե뤫ǡϤ

ǡ¿ϥܡɤϤΤݤޤ˥ե˥ǡ¸ߤʤСϤꤽľѤΤ
ǡ ץѹե뤫ǡϤˡ򶵤Ƥ

㤨Сdata.txt Ȥե˥ܡɤϤΤƱǡ񤤤Ƥäơץ̾ prg.rbȤ顤


ruby -w prg.rb < data.txt


Ȥ褦ˡ

ץεư
< ǡäե̾ ˤäĤ

ȡΥեȤܡϤ˻Ȥ롥

ޤƱͤ


cat data.txt | ruby -w prg.rb


Ȥ褦ˡ

ץεư
cat ǡäե̾ | ˤäĤ

ȤƤ⡤ΥեȤܡϤ˻Ȥ롥


| tee Ʊ˻Ȥ

ʤߤˡؤ | tee Ʊ˻Ȥϡ(ϥե̾ output.dat Ȳˤ)줾



ruby -w prg.rb < data.txt | tee output.dat
cat data.txt | ruby -w prg.rb | tee output.dat

ȤФ褤

notes.png ޤǤΥܡϤ򡤤٤ƥե뤫Ϥ夨ƤޤȤΤƤߤ褦
warning.png ե뤫Ϥξ硤֥ǡ⤦פȤΤϡ֥ե뤬ãפȤɽ롥


ž夲

n x m ΥǡϤ顤ιžֹ򤽤äݤɽץ񤳤
warning.png ǤդΥιФưʤȤʤ
ηˤʤäƤʤǡꤷʤƤɤ
ʤήȤƤ gets while ǻȤѥˤʤϤ

Ūˤϼ¹Ԥ

? : 1 2 5 8

? : 2 3 8 10

? :


Input matrix =

[ 1.0 2.0 5.0 8.0 ]

[ 2.0 3.0 8.0 10.0 ]


Transposed matrix =

[ 1.0 2.0 ]

[ 2.0 3.0 ]

[ 5.0 8.0 ]

[ 8.0 10.0 ]

(1ܤϤ "1 2 5 8" Ϥ2ܤϤ "2 3 8 10" Ϥ3ܤϤ Ctrl d Ʊ˲Ƥ)
Ȥ褦ư򤹤ץ񤱡ȤȤˤʤ롥

warning.png ˹ˤĤƽ񤤤ץबͤˤʤϤ褯ߤƤߤ褦


notes.png (Ը) n x n A Υǡ n٥ȥ b ΥǡϤȡϢΩ켡 Ax = b 򤤤 x Ϥץब񤱤̤ξõˡ狼äƤФǤϤƤ.

ݡ

ֵӹԤä½ˤĤơñˤޤȤ᤿򤻤.
ޤž夲ʬǼʬǽ񤤤ץ𤻤.

Ƽ

  1. °(ز)
  2. ֹ
  3. ǯ
  4. ̾
  5. οΥݡ(θȤˤĤƵŤ)

񤯤Τ˺ʤ褦.

about Icons, ClipArts

Some icons in this page are downloadable at ICONFINDER.

The "note" icon notes.png designed by Marco Martin is distributed with the LGPL licence,
the "warning" icon warning.png designed by Alexandre Moore with the GPL licence
and the "triangle" icon JNorth_arrow-right-sm.png designed by Joseph North is distributed with the Creative Commons (Attribution-Noncommercial-Share Alike 3.0 Unported) licence.

Some clip arts used in this page are downloadable at Open Clip Art Library.
We deeply appreciate their superb works. With licence, they describe that "the actual clipart content on open clipart library is Public domain" in the web.