fork download
  1. # くだすれPython(超初心者用) その31
  2. # http://e...content-available-to-author-only...h.net/test/read.cgi/tech/1476246889/381
  3.  
  4. $DEBUG = true
  5.  
  6. SRC = '11 + 45 * 1 - 4'
  7. ARR = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  8.  
  9. p SRC.split(/ +/).inject([0, []]) { |(i, xs), x|
  10. p ({:i => i, :x => x, :xs => xs}) if $DEBUG
  11.  
  12. if /[0-9]+/ =~ x then
  13. [i + 1, xs + [ARR[i].to_s]]
  14. else
  15. [i, xs + [x]]
  16. end
  17. }[1].join(' ')
Success #stdin #stdout 0.02s 9720KB
stdin
Standard input is empty
stdout
{:i=>0, :x=>"11", :xs=>[]}
{:i=>1, :x=>"+", :xs=>["1"]}
{:i=>1, :x=>"45", :xs=>["1", "+"]}
{:i=>2, :x=>"*", :xs=>["1", "+", "2"]}
{:i=>2, :x=>"1", :xs=>["1", "+", "2", "*"]}
{:i=>3, :x=>"-", :xs=>["1", "+", "2", "*", "3"]}
{:i=>3, :x=>"4", :xs=>["1", "+", "2", "*", "3", "-"]}
"1 + 2 * 3 - 4"