W
WuyaSea Operator
write a program to produce the following output:
1 A
2 BB
3 CCC
.......
25 YYYYYYYYYYYYYYYYYYYYYYYYY
26 ZZZZZZZZZZZZZZZZZZZZZZZZZZ
27 012345678901234567890123456
In java, this is probably what most of programmers do:
public class Text {
public static void main(String args[]) {
int x = 0;
for (char c = 'A'; c <= 'Z'; c++) {
x++;
System.out.print(x + " ");
for (int i = 0; i < x; i++) {
System.out.print( c );
}
System.out.println();
}
System.out.print(x + 1 + " ");
for (int i = 0; i <= x; i++) {
System.out.print(i % 10);
}
}
}
Same problem, can be tackled in ruby by just 3 lines.
n = ?A - 1
(1..26).each{|i| puts "#{i} #{(i+n).chr*i} "} # prints A-Z
puts "27 " + (0..27).collect{|i| i%10}.to_s # prints numbers line
# some ruby language functions
#
# ?<X> return the ASCII code of the character
# ?A => 65
# ?B => 66
# ?\n => 10 backspace ASCII code is 10
#
#
# <N>.chr returns the character from given ASCII code
# 65.chr => "A"
# 66.chr => "B"
# 10.chr => "\n"
#
#
# <STR>*<n> multiplies given string n times.
# "ABC"*2 => ABCABC
#
#
# ruby use "#{var}" syntax to print variable in a string
# x = "abc"
# puts "output: #{x}" => "output: abc"
If you're ready to learn ruby, and Ruby on Rails.
visit http://groups.wuyasea.com/group/ruby-on-rails
I'll answer all your ruby and rails questions.
Dorren
http://groups.wuyasea.com/profile/dorren
1 A
2 BB
3 CCC
.......
25 YYYYYYYYYYYYYYYYYYYYYYYYY
26 ZZZZZZZZZZZZZZZZZZZZZZZZZZ
27 012345678901234567890123456
In java, this is probably what most of programmers do:
public class Text {
public static void main(String args[]) {
int x = 0;
for (char c = 'A'; c <= 'Z'; c++) {
x++;
System.out.print(x + " ");
for (int i = 0; i < x; i++) {
System.out.print( c );
}
System.out.println();
}
System.out.print(x + 1 + " ");
for (int i = 0; i <= x; i++) {
System.out.print(i % 10);
}
}
}
Same problem, can be tackled in ruby by just 3 lines.
n = ?A - 1
(1..26).each{|i| puts "#{i} #{(i+n).chr*i} "} # prints A-Z
puts "27 " + (0..27).collect{|i| i%10}.to_s # prints numbers line
# some ruby language functions
#
# ?<X> return the ASCII code of the character
# ?A => 65
# ?B => 66
# ?\n => 10 backspace ASCII code is 10
#
#
# <N>.chr returns the character from given ASCII code
# 65.chr => "A"
# 66.chr => "B"
# 10.chr => "\n"
#
#
# <STR>*<n> multiplies given string n times.
# "ABC"*2 => ABCABC
#
#
# ruby use "#{var}" syntax to print variable in a string
# x = "abc"
# puts "output: #{x}" => "output: abc"
If you're ready to learn ruby, and Ruby on Rails.
visit http://groups.wuyasea.com/group/ruby-on-rails
I'll answer all your ruby and rails questions.
Dorren
http://groups.wuyasea.com/profile/dorren