Dividing by half in ruby to create an effective calculator - ruby

For the past while I've been working on a calculator, but have run into problems when needing to divide by a half. I'll add the offending bit of code along with a loop to keep it open below.
on = true
while on == true do
half = 1.0 / 2.0
puts ("FUNCTION IN TESTING MODE, DO NOT EXPECT IT TO FUNCTION PROPERLY")
puts ("Area of a triangle")
print("What is the legnth of the base? ").to_i
base = gets.chomp("base")
print("\nWhat is the height? ")
height = gets.chomp("height").to_i
PreAreaT = base * height
AreaT = PreAreaT * half
puts("The area of the triangle is #{AreaT}")
end
So essentially, how on Earth do I get the program to display an answer, rather than outputting nothing for the answer?
EDIT:As it would turn out the code above is improperly done. I've spent nearly two weeks asking myself why it wouldn't work only to find I had .to_i after a print statement rather than the input.

Your to_i call is switched around here.
print("What is the legnth of the base? ").to_i
base = gets.chomp("base")
Should be the other way 'round.
print("What is the length of the base? ")
base = gets.chomp("base").to_i
Further, chomp will attempt to remove any occurrences of base or height from the string. Be sure that you're intention is to remove those occurrences; if you want to remove whitespace, you'll have to take a different approach.

Related

Trouble building tree for possible chess piece movements

I'm building a chess game that takes a start coordinate and end coordinate and builds a tree using BFS. I have two methods to find possible moves.
The #find_possible_positions_from_origin method logs every single possible square that chess piece can end up (for example a rook can end up either, north up to 7 spaces, south up to 7 spaces, left up to 7 spaces, and right up to 7 spaces. If the end coordinate ends up BEING one of these coordinates from #find_possible_positions_from_origin, it then runs another method directly under it #find_possible_incremental_moves that traverses every single one of these spaces by only 1 space at a time - the reason being so that it can check if each space is empty, and not just hop over another piece. It creates a "cell" on each of these spaces so that it can log the path to destination. Each cell remembers the path to it from the starting coordinate.
I'm having trouble getting the second variable #find_possible_incremental_moves run smoothly under the #find_possible_positions_from_origin method. A "cell" is supposed to be created at the destination coordinate of the board, so that I can have the path to it, and check if the path is occupied by any other pieces. But when I call the destination coordinate, I get nil.
Someone corrected me on a previous question that I shouldn't post my entire production code which makes sense, so I included the single #build_tree that I'm having trouble with I hope that's enough.
Here is my code that have for this section.
def build_tree
#start_coor = convert_input_to_coor(#start)
#destination_coor = convert_input_to_coor(#destination)
#selected_piece = #gameboard[#start_coor[0]][#start_coor[1]]
starting_cell = Cell.new(#start_coor[0], #start_coor[1])
queue = []
queue.unshift(starting_cell)
until queue.empty?
current = queue.shift
possible_positions = find_possible_positions_from_origin([current.x, current.y])
next if possible_positions.nil?
if possible_positions.include?(#destination_coor)
#selected_piece.possible_positions = find_possible_incremental_moves(([current.x, current.y]))
#selected_piece.possible_positions.each do |move|
next unless !#dummyboard.board[move[0]][move[1]].instance_of? Cell
linked_cell = Cell.new(move[0], move[1])
current.edges << linked_cell
linked_cell.path = current.path + linked_cell.path
#dummyboard.board[move[0]][move[1]] = linked_cell
queue.push(linked_cell)
end
end
end
starting_cell
end

How to keep panning only if id exists?

I'm trying to pan through a list of elements to confirm that the set minimum number of elements is there on the screen. The problem I'm running into is that 'pan' will keep going and the test will pass even if I set the minimum to a high number (greater than what's actually there on the page).
When /^I swipe through my list of chat suggestion cards I should see the minimum$/ do
i = 0
while i < 12
i += 1
wait_for_element_exists("* id:'itemContainer'")
pan("* id:'itemContainer'", :left)
end
end
Is there anyway to check if the number of id: 'itemContainer' is actually there, to make it fail if the minimum number doesn't exist?
Query will only return the elements that are currently on the screen, so you will have to swipe across to make all of the cards visible to calabash and check for them. Assuming that query("* id:'itemContainer'") would only find one at a time, i.e. the cards are a whole screen in size,
12.times do
card_before_panning = query("* id:'itemContainer'")
pan("* id:'itemContainer'", :left)
card_after_panning = query("* id:'itemContainer'")
assert_not_equal card_before_panning, card_after_panning
end
If more than one of the cards can be visible on the screen at a time then you will have to do an additional check. Assuming that there could be two visible at the time
11.times do
card_before_panning = query("* id:'itemContainer' index:0")
pan("* id:'itemContainer'", :left)
card_after_panning = query("* id:'itemContainer' index:0")
assert_not_equal card_before_panning, card_after_panning
end
# Then check that there are two visible on the final screen.
assert_equal 2, query("* id:'itemContainer'").size
Note that this will only work if the cards appear differently in your app, i.e. they have different content. If query("* id:'itemContainer'") has identical results for each of the cards then calabash will not be able to tell them apart to see if anything has changed.

How do I get the line of text under the cursor in a TextView in gtk#?

I have a GTK# TextView and I want to read the line of text under the cursor. I don't see a single method that will do that, so I think I need to combine several method calls, like Buffer.GetText, Buffer.GetIterAtOffset, Buffer.CursorPosition, but it's not obvious to me what the right combination is.
TextIter are a bit odd to use. Buffer.CursorPosition gives you the current position.
It's easy to find the end of the line:
var end = Buffer.CursorPosition;
end.ForwardToLineEnd();
To get the first character, there's not symetrical method, so you might try:
var start = Buffer.CursorPosition;
start.BackwardChars(start.LineOffset); // LineOffset gives you the iter offset on the current line.

ZPL - zebra: print justified text block without overwriting last line

I'm using the following command to print a justified text:
^FB1800,3,0,J^FT100,200^A0B,26,26^FH\^FDLONG TEXT TO BE PRINTED, WHICH DOESNT FIT IN ONLY 3 LINES...^FS
The command ^FB1800,3,0,J prints a field block in a width of 1800 dots, maximum 3 lines, justified.
The problem is that if the text exceeds the maximum number of lines, it overwrites the last line! :( That of course makes the text of the last line unreadable.
How can I avoid that? Does anybody know if is there a way to cut the exceeding text?
The documentation says exactly that this happens:
Text exceeding the maximum number of lines overwrites the last line. Changing the font size automatically increases or decreases the size of the block.
For reference: I'm using printer Zebra 220Xi4.
Any help would be appreciated. Thank you!
Take a look at the ^TB command. It is preferred over the ^FB command and truncates if the text exceeds the size defined in the TB params
I had just about the same problem, what fixed it in my case - although not the most elegant way - is to specify a higher number of maximum lines, and then formatting it in a way that only the first 3 are in the visible area.
In your case it would be for example ^FB1800,7,0,J instead of ^FB1800,3,0,J
This at least fixed it for me right away, because I print this text at the bottom of the label. If you need to have it somewhere in the middle or top, there might be some tricks with putting a (white) box on top of the overflow-area, since the Zebra printers seem to render before printing. Hope it helps.
Depending on the higher-level programming language you're using (assuming that you are), you could accomplish the same thing (truncate the text to be printed to a specified number of characters) with code like this (C# shown here):
public void PrintLabel(string price, string description, string barcode)
{
const int MAX_CAPS_DESC_LEN = 21;
const int MAX_LOWERCASE_DESC_LEN = 32;
try
{
bool descAllUpper = HHSUtils.IsAllUpper(description);
if (descAllUpper)
{
if (description.Length > MAX_CAPS_DESC_LEN)
{
description = description.Substring(0, MAX_CAPS_DESC_LEN);
}
}
else // not all upper
{
if (description.Length > MAX_LOWERCASE_DESC_LEN)
{
description = description.Substring(0, MAX_LOWERCASE_DESC_LEN);
}
}
. . .
This is what I'm using; is there any reason to prefer the "raw" ^TB command over this?

Algorithm help: Fit a text blurb to its textbox by length

I have a rather unusual problem, and it is hurting my brain.
Problem: Given a textbox of known length, and the text that will go inside it, make the text "fit" by truncating it with room for "..." to fit inside the box. (Context : This is for ASP.NET C#, but I think the algorithm is language agnostic.)
Example : [_________]
Text : The big brown dog jumped over the red fence.
Solution :[The bi...]
Example : [_________]
Text : Ferret
Solution :[Ferret___]
Given:
// Returns the number of px (as an int) that the arg text is in length
public int textLength(String theText, float emSize)
Question: What is the simplest and fastest way to do this?
I am afraid to do it by hacking off one character at a time, adding "..." and then checking the length because some of the strings to fit are veeeeery long.
You could do a binary search on the correct length instead, which means you only have to try log(n) sizes.
Oh, also if the text is monospaced (every character is given the width of an em) then it's pretty easy to figure this out programatically:
if str.length * emWidth < textBoxWidth
tb.text = str
else
tb.text = substring(str, 0, round_down(textBoxWidth / emWidth) - 3) + "..."
Why start from end? Start from the beginning and add letters (and ...) until it no longer fits. :)

Resources