Tic Tac Toe Game pseudocode - pseudocode

Alright, so basically I am to create a tic tac toe game in pseudocode. Which sucks because I can't see output. Anyways,
/*
*Program by: Cory
*Program: Tic Tac Toe game
*
/
Module Main()
Boolean player = true
Constant Integer ROWS = 3, COLS = 3
Declare String values[ROWS][COLS] = "", "", "",
"", "", "",
"", "", ""
Call Module getIntro()
Call Module showBoard(values[][], ROWS, COLS)
Call Module playGame(values[][], COLS, ROWS, player)
End Module
Module getIntro()
Display "Hello, this is a simple tic tac toe game."
Display "It will rotate turns between players one and two,"
Display "While 3,3 would be the bottom right."
Display "Player 1 is X and player 2 is O"
End Module
Module showBoard(String values[][], Integer rows, Integer cols)
Declare Integer row, col
For row = 0 to rows - 1
For col = 0 to cols - 1
Display values[rows][cols]
End For
End For
End Module
Module playGame(String values[][], Integer cols, Integer rows, Boolean player) //places moves, checks for already placed moves
Declare Integer row, col
player = true
For row = 0 to rows - 1
For col = 0 to cols -1
If (player == true)
Display "First player's turn, please input your move"
Input values[row][col]
if (checkMove(values[][], rows, cols) == false)
Display "Invalid move, please try again"
Display "First player's turn again, please input your move"
Input values[row][col]
End If
values[row][col] = "X"
showBoard(values[row][col], rows, cols)
checkWin()
player = false
Else If (player == false)
Display "Second player's turn, please input your move"
Input values[row][col]
if (checkMove(values[][], rows, cols) == false)
Display "Invalid move, please try again"
Display "Second player's turn again, please input your move"
Input values[row][col]
End If
values[row][col] = "O"
showBoard(values[row][col], rows, cols)
checkWin()
player = true
End For
End For
End Module
Function Boolean checkMove(String values[][], Integer cols, Integer rows)
Declare Integer row, col
For row = 0 to rows - 1
For col = 0 to cols - 1
if (values[row][col] != "*")
Display "Player has already placed a move there"
End If
End For
End For
return false
End Function
Module checkWin()
checkRow(values[][], 3)
checkCol(values[][], 3)
checkDiagonal(values[][], 3, 3)
End Module
Module checkRow(String values[][], Integer rows) //checks horizontal win
Declare Integer row, col
For row = 0 to rows - 1
if (values[row][0] == "X")
Display "Player one has won!"
//player1Win = true
Else if (values[row][0] == "O")
Display "Player two has won!"
//player1Win = false
End If
End For
End Module
Module checkCol(String values[][], Integer cols) //checks vertical win
Declare Integer row, col
For col = 0 to cols -1
if (values[0][col] == "X")
Display "Player one has won!"
//player1Win = true
Else if (values[0][col] == "O")
Display "Player two has won!"
//player1Win = false
End If
End For
End Module
Module checkDiagonal(String values[][], Integer cols, Integer rows, Boolean player1Win) //checks Diagonal win
So 1) please look over my code and tell me if I'm doing something wrong or if there's a way I can make it look better
2) I need help checking for diagonal wins.
3) checking for ties

Answering it accordingly:
There are lots of modifications to be done in your code regarding condition checking and other "cases" which might result while playing the game. It will be better if you ask your question a bit more specifically.
As far as the diagonal check is concerned, do the same as in checkCol and checkRow, instead check for values[index][index], where index value will be same for both row and column.
For tie checking you can keep a flag variable which will return false when no rows, columns and diagonals match.
Update:
For the opposite diagonal, you have to check for values[index][SIZE-index-1].

Related

I'm having a bit of trouble making this tic-tac-toe algorithm work. It's an algorithm for checking if a move makes a win

This works really well with diagonals and the third row or third column. For some reason, when someone wins, either X or O, on the first and second row or columns, no win is signaled. Row and Col resemble the place where the play was made, and player resembles who did the play.
What's making my mind boggle is the fact that it doesn't recognize a win on all rows and columns, seems so weird to me that i'm just missing where the problem might be.
--[[
Function that checks if there is a win whenever a play is made
]]
function checkWin(row, col, player)
-- check rows. I have the row that the marker was placed. Go through it and see if there is a win.
for c = 1, 3 do
if board[row][c] ~= player then
break
end
if c == 3 then
gWhoWon = player
gameOver = true
end
end
-- check columns. I have the col that the marker was placed. Go through it and see if there is a win.
for r = 1, 3 do
if board[r][col] ~= player then
break
end
if r == 3 then
gWhoWon = player
gameOver = true
end
end
-- check diagonals. Check both diagonals and see if there is a win (can make this more efficient, only looking at
-- diagonals that matter).
-- first diag
for d = 1, 3 do
if board[d][d] ~= player then
break
end
if d == 3 then
gWhoWon = player
gameOver = true
end
end
-- anti diag
for d = 1, 3 do
if board[d][4-d] ~= player then
break
end
if d == 3 then
gWhoWon = player
gameOver = true
end
end
--[[
if turn == 9 then
gameOver = true
end
]]
end
Here's the function in which I initialize my board and where I call the checkWin function:
function PlayState:init()
xTurn = true
gameOver = false
turn = 1
gWhoWon = ""
-- board
board = {
{"", "", ""},
{"", "", ""},
{"", "", ""}
}
end
function PlayState:update(dt)
-- transition to win state when someone wins
if gameOver then
gStateMachine:change('win')
end
-- if there is a left mouse click, insert an "x" or an "o" on that square
if love.mouse.wasPressed(1) then
-- get mouse position
x = love.mouse.getX()
y = love.mouse.getY()
-- iterate through the board
for row = 1, 3 do
for col = 1, 3 do
-- if mouse click was inside a square and it is empty
if isInsideSquare(x, y, row, col) and isEmpty(row, col) then
-- put an "x" on the square
if xTurn then
board[col][row] = 'x'
checkWin(row, col, 'x') -- check if this move makes a win
xTurn = false
-- put an "o" on the square
else
board[col][row] = 'o'
checkWin(row, col, 'o') -- check if this move makes a win
xTurn = true
end
turn = turn + 1
end
end
end
end
end
There is a mix-up in using row and column values for the dimensions of the board matrix:
In the update method you have code like this: board[col][row] = 'x', so where the column is the first dimension of the matrix, but:
In the checkWin function, you have code like board[row][c] ~= player, so where the column is the second dimension of the matrix
This will lead to unexpected results. Make sure to harmonise your code so that each of the two array dimensions are always used for the same axis (row versus column).

Binary Search error when doesn't find a number

I've created a binary search, while looking at the online wiki. I have a class of Athletes that each have a name and number. I'm inputting a file text or csv, doesn't matter - with each of the athletes name's and numbers. My program sorts them first, and then I am trying to add the functionality of searching for a number based off of user input, and displaying who wears that numbered jersey. So my initial post was trying to binary search for people with the same number. As in, if I had Michael Jordan and Lebron James on my list, they both wear 23 - so when my search goes through it would only output 1 (whichever it comes to first). I was looking for how to make my search (below) accept/find multiple occurrences of a number. However, upon further testing, I found that if I input a number not actually in my list it would give me the error: search: stack level too deep which I don't know what that means. I think my search doesn't handle properly if there's no instance of the number, or if the array is 0.
So I was looking for some help to see how I can fix this to work if the number input by a user isn't in the list. So if someone inputs "1000" -- no one has worn that jersey number and should return false. Or something of that sort, break, whatever.
def search(array, num, start = 0, last = nil)
if last == nil
last = array.count - 1
end
mid = (start + last) / 2
if num < array[mid].number
return search(array, num, start, mid - 1)
elsif num > array[mid].number
return search(array, num, mid + 1, last)
else
return mid
end
end
I've now also gotten ==: stack level too deep on the line where if last == nil
This is not the ruby way of doing things. When you have a collection, and you would like to only select some of them based on a certain condition, The ruby approach would be to use Enumerable#select
You would ideally have some array of athletes like so
athletes = [Athlete.new, Athlete.new]
athletes_with_number_23 = athletes.select { |athlete| athlete.number == 23 } #if you want all
first_athlete_wearing_23 = athletes.detect { |athlete| athlete.number == 23 } #if you want only the first one
Disclaimer: this is pseudo code.
I changed my search function:
def search(array, key)
lo = 0
hi = array.length-1
while(lo <= hi)
mid = lo + ((hi-lo)/2)
if array[mid].number == key
return mid
elsif array[mid].number < key
lo = mid + 1
else
hi = mid - 1
end
end
puts "Value not found in array"
end
If I got u right you wanna get ALL athletes with a specific number in a sorted list.
First. Your code is a way too procedural, "This is not the ruby way of doing things.". But I guess it doesn't matter for you.
So, I suggest you find an index of one of the athletes and just walk array left and right from it to collect same-number mans.
Here my procedural version. It uses your function.
def athletes_with_number(athletes, number)
result = []
found_index = search(athletes, number)
return result unless found_index
# walk left
i = found_index
while i >= 0 && athletes[i].number == number
result << athletes[i]
i -= 1
end
# walk right
i = found_index + 1 # athletes[found_index] already added
while i < athletes.size && athletes[i].number == number
result << athletes[i]
i += 1
end
result
end
def search(array, key)
lo = 0
hi = array.length-1
while(lo <= hi)
mid = lo + ((hi-lo)/2)
if array[mid].number == key
return mid
elsif array[mid].number < key
lo = mid + 1
else
hi = mid - 1
end
end
nil
end

Ruby armstrong numbers in a range

puts "Enter range(starts at 1), ends at the number that you enter: "
range = gets.chomp.to_i
number = 1
while number <= range
temporary_number = number
sum_angstrom = 0
number += number
while(temporary_number != 0)
digit = temporary_number % 10
temporary_number /= 10
sum_angstrom = sum_angstrom + (digit ** 3)
end
if (sum_angstrom == number)
puts number
end
end
This time, I tried to make a program to show the armstrong numbers in a range that's taken from the user's input. The program just stops after I enter the number and press enter and i can't figure out why.
Keep in mind that i can't use for(each), that's why i'm using while so often.
First of all, change number += number to number += 1; otherwise you will only test the powers of 2.
Second, move the number += 1 line at the bottom of the while block it is in. Otherwise you will always test if sum_armstrong(n) == n+1.
This works:
puts "Enter range(starts at 1), ends at the number that you enter: "
range = gets.chomp.to_i
number = 1
while number <= range
temporary_number = number
sum_angstrom = 0
while(temporary_number != 0)
digit = temporary_number % 10
temporary_number /= 10
sum_angstrom = sum_angstrom + (digit ** 3)
end
if (sum_angstrom == number)
puts number
end
number += 1
end
Armstrong Number in Ruby one liner
n = 153
s = 0
n.to_s.split("").map{|e| s+=(e.to_i*e.to_i*e.to_i)}
puts (n==s ? "Armstrong number" : "Not Armstrong number")
You can iterate in a range to print the value based on your requirement.
Main logic lies in below line.
n.to_s.split("").map{|e| s+=(e.to_i*e.to_i*e.to_i)}
Improving my answer a little bit
n.digits.map{|e| s+=(e**3)}

How do I describe all integers in Ruby

I'm trying to write a basic program that spits out the English version of a number when the user inputs a numeral:
input = 44
output = fourty four
Is there a way to describe all integers?
Basically I want the execution to look something like:
number = gets.chomp
if number != (whatever the nomenclature is for integer)
puts 'Please enter a positive number'
or something to that effect.
You can do that with the numbers_and_words gem:
https://github.com/kslazarev/numbers_and_words
It supports languages other than english as well.
For example:
21.to_words
=> "twenty-one"
44.to_words
=> "forty-four"
I modified the Fixnum class and added a method in_words. What I did is I broke each number up into groups of three, so 100000 turns into [100, 000] and 123456789 turns into [123, 456, 789] or 1543 turns into [1, 453] then I went element by element and named every number in the element and added the appropriate word, like hundred and thousand. If you have any questions I am happy to explain!
class Fixnum
LOW = %w(zero one two three four five six seven
eight nine ten eleven twelve thirteen fourteen
fifteen sixteen seventeen eighteen nineteen)
TWO_DIGIT = %w(ten twenty thirty forty fifty sixty seventy eighty ninety)
BIG_NUMS = %w(hundred thousand million billion trillion)
def in_words
# Break up number into bytes with three bits each
# Then turn each byte into words
# Break into bytes
number = self.to_s.reverse
bytes = []
num_bytes = (number.length.to_f / 3.0).ceil()
num_bytes.times { |x| bytes << number[(x*3)..(x*3)+2].reverse }
#puts bytes.reverse.join(",")
# Turn bytes into words bit by bit
word = []
text = ""
bytes.each_with_index do |byte, i|
text = ""
# First bit
text = LOW[byte[0].to_i] if (byte.length == 3 && byte[0].to_i != 0) || byte.length == 1
# Add hundred if 3 bits
text += " hundred" if byte.length == 3 && byte[0].to_i != 0
# Second bit
if byte.length == 3 # Three bits
if byte[1].to_i > 1 # Second bit greater than teens
text += " " + TWO_DIGIT[byte[1].to_i + (-1)]
elsif byte[1].to_i != 0 # Second bit not zero
text += " " + LOW[byte[1..2].to_i]
end
elsif byte.length == 2 # Two bits
if byte[0].to_i > 1 # Greater than teens
text += " " + TWO_DIGIT[byte[0].to_i + (-1)]
text += " " + LOW[byte[1].to_i] if byte[1].to_i != 0
else # Less than twenty
text += LOW[byte[0..1].to_i]
end
end
# Third bit if three bytes and second bit > teens and third bit nonzero
text += " " + LOW[byte[2].to_i] if byte[1].to_i != 1 && byte[2].to_i > 0 && byte.length > 2
# Add trillion/billion/million/thousand
text += " " + BIG_NUMS[i] if i != 0 && byte.to_i != 0
word << text.strip if text.strip != ""
end
word.reverse.join(" ")
end
end
Because I modified the Fixnum object, you can call this from any Fixnum e.g. 44.in_words
EDIT: It looks like you might be trying to check input for integers. I would recommend making a function to handle that:
def check_input(i)
if !(i =~ /^[0-9]+$/)
puts "Sorry, that is an invalid input! Please try again"
i = check_input(gets.chomp)
end
i.to_i
end
I think the best way to handle that is with regex (pattern matching). Basically your function checks if the input isn't a number, then it asks for input again. If it is a number, then the function returns the number. /^[0-9]+$/ is the regex. ^ means start of the line and $ means end of the line. [0-9] matches any digit zero through nine (as the Tin Man commented, you can also use \d to represent any digit and it is equivalent), and + means match the previous thing (any digit) at least once.

Number crunching in Ruby (optimisation needed)

Ruby may not be the optimal language for this but I'm sort of comfortable working with this in my terminal so that's what I'm going with.
I need to process the numbers from 1 to 666666 so I pin out all the numbers that contain 6 but doesn't contain 7, 8 or 9. The first number will be 6, the next 16, then 26 and so forth.
Then I needed it printed like this (6=6) (16=6) (26=6) and when I have ranges like 60 to 66 I need it printed like (60 THRU 66=6) (SPSS syntax).
I have this code and it works but it's neither beautiful nor very efficient so how could I optimize it?
(silly code may follow)
class Array
def to_ranges
array = self.compact.uniq.sort
ranges = []
if !array.empty?
# Initialize the left and right endpoints of the range
left, right = array.first, nil
array.each do |obj|
# If the right endpoint is set and obj is not equal to right's successor
# then we need to create a range.
if right && obj != right.succ
ranges << Range.new(left,right)
left = obj
end
right = obj
end
ranges << Range.new(left,right) unless left == right
end
ranges
end
end
write = ""
numbers = (1..666666).to_a
# split each number in an array containing it's ciphers
numbers = numbers.map { |i| i.to_s.split(//) }
# delete the arrays that doesn't contain 6 and the ones that contains 6 but also 8, 7 and 9
numbers = numbers.delete_if { |i| !i.include?('6') }
numbers = numbers.delete_if { |i| i.include?('7') }
numbers = numbers.delete_if { |i| i.include?('8') }
numbers = numbers.delete_if { |i| i.include?('9') }
# join the ciphers back into the original numbers
numbers = numbers.map { |i| i.join }
numbers = numbers.map { |i| i = Integer(i) }
# rangify consecutive numbers
numbers = numbers.to_ranges
# edit the ranges that go from 1..1 into just 1
numbers = numbers.map do |i|
if i.first == i.last
i = i.first
else
i = i
end
end
# string stuff
numbers = numbers.map { |i| i.to_s.gsub(".."," thru ") }
numbers = numbers.map { |i| "(" + i.to_s + "=6)"}
numbers.each { |i| write << " " + i }
File.open('numbers.txt','w') { |f| f.write(write) }
As I said it works for numbers even in the millions - but I'd like some advice on how to make prettier and more efficient.
I deleted my earlier attempt to parlez-vous-ruby? and made up for that. I know have an optimized version of x3ro's excellent example.
$,="\n"
puts ["(0=6)", "(6=6)", *(1.."66666".to_i(7)).collect {|i| i.to_s 7}.collect do |s|
s.include?('6')? "(#{s}0 THRU #{s}6=6)" : "(#{s}6=6)"
end ]
Compared to x3ro's version
... It is down to three lines
... 204.2 x faster (to 66666666)
... has byte-identical output
It uses all my ideas for optimization
gen numbers based on modulo 7 digits (so base-7 numbers)
generate the last digit 'smart': this is what compresses the ranges
So... what are the timings? This was testing with 8 digits (to 66666666, or 823544 lines of output):
$ time ./x3ro.rb > /dev/null
real 8m37.749s
user 8m36.700s
sys 0m0.976s
$ time ./my.rb > /dev/null
real 0m2.535s
user 0m2.460s
sys 0m0.072s
Even though the performance is actually good, it isn't even close to the C optimized version I posted before: I couldn't run my.rb to 6666666666 (6x10) because of OutOfMemory. When running to 9 digits, this is the comparative result:
sehe#meerkat:/tmp$ time ./my.rb > /dev/null
real 0m21.764s
user 0m21.289s
sys 0m0.476s
sehe#meerkat:/tmp$ time ./t2 > /dev/null
real 0m1.424s
user 0m1.408s
sys 0m0.012s
The C version is still some 15x faster... which is only fair considering that it runs on the bare metal.
Hope you enjoyed it, and can I please have your votes if only for learning Ruby for the purpose :)
(Can you tell I'm proud? This is my first encounter with ruby; I started the ruby koans 2 hours ago...)
Edit by #johndouthat:
Very nice! The use of base7 is very clever and this a great job for your first ruby trial :)
Here's a slight modification of your snippet that will let you test 10+ digits without getting an OutOfMemory error:
puts ["(0=6)", "(6=6)"]
(1.."66666666".to_i(7)).each do |i|
s = i.to_s(7)
puts s.include?('6') ? "(#{s}0 THRU #{s}6=6)" : "(#{s}6=6)"
end
# before:
real 0m26.714s
user 0m23.368s
sys 0m2.865s
# after
real 0m15.894s
user 0m13.258s
sys 0m1.724s
Exploiting patterns in the numbers, you can short-circuit lots of the loops, like this:
If you define a prefix as the 100s place and everything before it,
and define the suffix as everything in the 10s and 1s place, then, looping
through each possible prefix:
If the prefix is blank (i.e. you're testing 0-99), then there are 13 possible matches
elsif the prefix contains a 7, 8, or 9, there are no possible matches.
elsif the prefix contains a 6, there are 49 possible matches (a 7x7 grid)
else, there are 13 possible matches. (see the image below)
(the code doesn't yet exclude numbers that aren't specifically in the range, but it's pretty close)
number_range = (1..666_666)
prefix_range = ((number_range.first / 100)..(number_range.last / 100))
for p in prefix_range
ps = p.to_s
# TODO: if p == prefix_range.last or p == prefix_range.first,
# TODO: test to see if number_range.include?("#{ps}6".to_i), etc...
if ps == '0'
puts "(6=6) (16=6) (26=6) (36=6) (46=6) (56=6) (60 thru 66) "
elsif ps =~ /7|8|9/
# there are no candidate suffixes if the prefix contains 7, 8, or 9.
elsif ps =~ /6/
# If the prefix contains a 6, then there are 49 candidate suffixes
for i in (0..6)
print "(#{ps}#{i}0 thru #{ps}#{i}6) "
end
puts
else
# If the prefix doesn't contain 6, 7, 8, or 9, then there are only 13 candidate suffixes.
puts "(#{ps}06=6) (#{ps}16=6) (#{ps}26=6) (#{ps}36=6) (#{ps}46=6) (#{ps}56=6) (#{ps}60 thru #{ps}66) "
end
end
Which prints out the following:
(6=6) (16=6) (26=6) (36=6) (46=6) (56=6) (60 thru 66)
(106=6) (116=6) (126=6) (136=6) (146=6) (156=6) (160 thru 166)
(206=6) (216=6) (226=6) (236=6) (246=6) (256=6) (260 thru 266)
(306=6) (316=6) (326=6) (336=6) (346=6) (356=6) (360 thru 366)
(406=6) (416=6) (426=6) (436=6) (446=6) (456=6) (460 thru 466)
(506=6) (516=6) (526=6) (536=6) (546=6) (556=6) (560 thru 566)
(600 thru 606) (610 thru 616) (620 thru 626) (630 thru 636) (640 thru 646) (650 thru 656) (660 thru 666)
(1006=6) (1016=6) (1026=6) (1036=6) (1046=6) (1056=6) (1060 thru 1066)
(1106=6) (1116=6) (1126=6) (1136=6) (1146=6) (1156=6) (1160 thru 1166)
(1206=6) (1216=6) (1226=6) (1236=6) (1246=6) (1256=6) (1260 thru 1266)
(1306=6) (1316=6) (1326=6) (1336=6) (1346=6) (1356=6) (1360 thru 1366)
(1406=6) (1416=6) (1426=6) (1436=6) (1446=6) (1456=6) (1460 thru 1466)
(1506=6) (1516=6) (1526=6) (1536=6) (1546=6) (1556=6) (1560 thru 1566)
(1600 thru 1606) (1610 thru 1616) (1620 thru 1626) (1630 thru 1636) (1640 thru 1646) (1650 thru 1656) (1660 thru 1666)
etc...
Note I don't speak ruby, but I intend to dohave done a ruby version later just for speed comparison :)
If you just iterate all numbers from 0 to 117648 (ruby <<< 'print "666666".to_i(7)') and print them in base-7 notation, you'll at least have discarded any numbers containing 7,8,9. This includes the optimization suggestion by MrE, apart from lifting the problem to simple int arithmetic instead of char-sequence manipulations.
All that remains, is to check for the presence of at least one 6. This would make the algorithm skip at most 6 items in a row, so I deem it less unimportant (the average number of skippable items on the total range is 40%).
Simple benchmark to 6666666666
(Note that this means outputting 222,009,073 (222M) lines of 6-y numbers)
Staying close to this idea, I wrote this quite highly optimized C code (I don't speak ruby) to demonstrate the idea. I ran it to 282475248 (congruent to 6666666666 (mod 7)) so it was more of a benchmark to measure: 0m26.5s
#include <stdio.h>
static char buf[11];
char* const bufend = buf+10;
char* genbase7(int n)
{
char* it = bufend; int has6 = 0;
do
{
has6 |= 6 == (*--it = n%7);
n/=7;
} while(n);
return has6? it : 0;
}
void asciify(char* rawdigits)
{
do { *rawdigits += '0'; }
while (++rawdigits != bufend);
}
int main()
{
*bufend = 0; // init
long i;
for (i=6; i<=282475248; i++)
{
char* b7 = genbase7(i);
if (b7)
{
asciify(b7);
puts(b7);
}
}
}
I also benchmarked another approach, which unsurprisingly ran in less than half the time because
this version directly manipulates the results in ascii string form, ready for display
this version shortcuts the has6 flag for deeper recursion levels
this version also optimizes the 'twiddling' of the last digit when it is required to be '6'
the code is simply shorter...
Running time: 0m12.8s
#include <stdio.h>
#include <string.h>
inline void recursive_permute2(char* const b, char* const m, char* const e, int has6)
{
if (m<e)
for (*m = '0'; *m<'7'; (*m)++)
recursive_permute2(b, m+1, e, has6 || (*m=='6'));
else
if (has6)
for (*e = '0'; *e<'7'; (*e)++)
puts(b);
else /* optimize for last digit must be 6 */
puts((*e='6', b));
}
inline void recursive_permute(char* const b, char* const e)
{
recursive_permute2(b, b, e-1, 0);
}
int main()
{
char buf[] = "0000000000";
recursive_permute(buf, buf+sizeof(buf)/sizeof(*buf)-1);
}
Benchmarks measured with:
gcc -O4 t6.c -o t6
time ./t6 > /dev/null
$range_start = -1
$range_end = -1
$f = File.open('numbers.txt','w')
def output_number(i)
if $range_end == i-1
$range_end = i
elsif $range_start < $range_end
$f.puts "(#{$range_start} thru #{$range_end})"
$range_start = $range_end = i
else
$f.puts "(#{$range_start}=6)" if $range_start > 0 # no range, print out previous number
$range_start = $range_end = i
end
end
'1'.upto('666') do |n|
next unless n =~ /6/ # keep only numbers that contain 6
next if n =~ /[789]/ # remove nubmers that contain 7, 8 or 9
output_number n.to_i
end
if $range_start < $range_end
$f.puts "(#{$range_start} thru #{$range_end})"
end
$f.close
puts "Ruby is beautiful :)"
I came up with this piece of code, which I tried to keep more or less in FP-styling. Probably not much more efficient (as it has been said, with basic number logic you will be able to increase performance, for example by skipping from 19xx to 2000 directly, but that I will leave up to you :)
def check(n)
n = n.to_s
n.include?('6') and
not n.include?('7') and
not n.include?('8') and
not n.include?('9')
end
def spss(ranges)
ranges.each do |range|
if range.first === range.last
puts "(" + range.first.to_s + "=6)"
else
puts "(" + range.first.to_s + " THRU " + range.last.to_s + "=6)"
end
end
end
range = (1..666666)
range = range.select { |n| check(n) }
range = range.inject([0..0]) do |ranges, n|
temp = ranges.last
if temp.last + 1 === n
ranges.pop
ranges.push(temp.first..n)
else
ranges.push(n..n)
end
end
spss(range)
My first answer was trying to be too clever. Here is a much simpler version
class MutablePrintingCandidateRange < Struct.new(:first, :last)
def to_s
if self.first == nil and self.last == nil
''
elsif self.first == self.last
"(#{self.first}=6)"
else
"(#{self.first} thru #{self.last})"
end
end
def <<(x)
if self.first == nil and self.last == nil
self.first = self.last = x
elsif self.last == x - 1
self.last = x
else
puts(self) # print the candidates
self.first = self.last = x # reset the range
end
end
end
and how to use it:
numer_range = (1..666_666)
current_range = MutablePrintingCandidateRange.new
for i in numer_range
candidate = i.to_s
if candidate =~ /6/ and candidate !~ /7|8|9/
# number contains a 6, but not a 7, 8, or 9
current_range << i
end
end
puts current_range
Basic observation: If the current number is (say) 1900 you know that you can safely skip up to at least 2000...
(I didn't bother updating my C solution for formatting. Instead I went with x3ro's excellent ruby version and optimized that)
Undeleted:
I still am not sure whether the changed range-notation behaviour isn't actually what the OP wants: This version changes the behaviour of breaking up ranges that are actually contiguous modulo 6; I wouldn't be surprised the OP actually expected
.
....
(555536=6)
(555546=6)
(555556 THRU 666666=6)
instead of
....
(666640 THRU 666646=6)
(666650 THRU 666656=6)
(666660 THRU 666666=6)
I'll let the OP decide, and here is the modified version, which runs in 18% of the time as x3ro's version (3.2s instead of 17.0s when generating up to 6666666 (7x6)).
def check(n)
n.to_s(7).include?('6')
end
def spss(ranges)
ranges.each do |range|
if range.first === range.last
puts "(" + range.first.to_s(7) + "=6)"
else
puts "(" + range.first.to_s(7) + " THRU " + range.last.to_s(7) + "=6)"
end
end
end
range = (1..117648)
range = range.select { |n| check(n) }
range = range.inject([0..0]) do |ranges, n|
temp = ranges.last
if temp.last + 1 === n
ranges.pop
ranges.push(temp.first..n)
else
ranges.push(n..n)
end
end
spss(range)
My answer below is not complete, but just to show a path (I might come back and continue the answer):
There are only two cases:
1) All the digits besides the lowest one is either absent or not 6
6, 16, ...
2) At least one digit besides the lowest one includes 6
60--66, 160--166, 600--606, ...
Cases in (1) do not include any continuous numbers because they all have 6 in the lowest digit, and are different from one another. Cases in (2) all appear as continuous ranges where the lowest digit continues from 0 to 6. Any single continuation in (2) is not continuous with another one in (2) or with anything from (1) because a number one less than xxxxx0 will be xxxxy9, and a number one more than xxxxxx6 will be xxxxxx7, and hence be excluded.
Therefore, the question reduces to the following:
3)
Get all strings between "" to "66666" that do not include "6"
For each of them ("xxx"), output the string "(xxx6=6)"
4)
Get all strings between "" to "66666" that include at least one "6"
For each of them ("xxx"), output the string "(xxx0 THRU xxx6=6)"
The killer here is
numbers = (1..666666).to_a
Range supports iterations so you would be better off by going over the whole range and accumulating numbers that include your segments in blocks. When one block is finished and supplanted by another you could write it out.

Resources