Open Side Menu Go to the Top

06-16-2016 , 10:02 AM
In the first example, you're using to_sym method run on convert to return a value that is passed as a parameter to the push method run on symbols.

In the second, you're running to_sym on convert. This may or may not affect the value of convert, similar to how some methods have a bang (!) or exclamation that means "run this code and apply it/make the changes take effect on the item it was called on".

So if I'm understanding the second code, your to_sym returns a value but does nothing with that value. Then the original value of convert is pushed to symbols.

Does that make sense?

It would be like

a = 5
a+a
array.push(a) # gives you 5 because a+a wasn't assigned to anything, it was just code that ran and returned a value to nowhere

If I want 10 in this case, I need array.push(a+a), as a+a returns 10.

To_sym returns a symbol*, but you're not putting that symbol anywhere in your second snippet.

*http://ruby-doc.org/core-2.2.3/Symbo...ethod-i-to_sym

Last edited by Loki; 06-16-2016 at 10:07 AM.
Programming homework and newbie help thread Quote
Programming homework and newbie help thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Programming homework and newbie help thread
06-16-2016 , 10:12 AM
Quote:
Originally Posted by Noodle Wazlib
Spoiler:
In the first example, you're using to_sym method run on convert to return a value that is passed as a parameter to the push method run on symbols.

In the second, you're running to_sym on convert. This may or may not affect the value of convert, similar to how some methods have a bang (!) or exclamation that means "run this code and apply it/make the changes take effect on the item it was called on".

So if I'm understanding the second code, your to_sym returns a value but does nothing with that value. Then the original value of convert is pushed to symbols.

Does that make sense?

It would be like

a = 5
a+a
array.push(a) # gives you 5 because a+a wasn't assigned to anything, it was just code that ran and returned a value to nowhere

If I want 10 in this case, I need array.push(a+a), as a+a returns 10.

To_sym returns a symbol*, but you're not putting that symbol anywhere in your second snippet.

*http://ruby-doc.org/core-2.2.3/Symbo...ethod-i-to_sym
It does make sense, thank you!

P.S Looks like the spoiler tag might need some additional coding :P
Programming homework and newbie help thread Quote
06-16-2016 , 12:27 PM
C++ question related to the break statement. Here is the code in question:
Code:
while (!infile.eof());
	{
		getColumnValue(infile, strTheme, strTitle, strArtist, strYear, strURL);

		switch(eColumn)
		{
		case THEME:
			if (strTheme == strItem)
			{
				cout << endl << "#" << currentRecordNum << endl;
				cout << "Title: " << strTitle << endl;
				cout << "Artist: " << strArtist << endl;
				cout << "Year: " << strYear << endl << endl;
				recordMatched = true;
				totalMatched++;
			}
			break;

		case TITLE:
			//
			break;

		case ARTIST:
			//
			break;

		case YEAR:
			//
			break;

		default:
			return -1;
		}

		// Some statistics
		// Keep track of the last row (record) read from the CSV file.
		currentRecordNum++;

		// We need to stop our looping if two things occur
		// 1) We found at least one record to display on this page.
		// 2) and we looped through one page of rows from the CSV file.

		if (recordMatched == true && recordsPerPageCount == recordsPerPageRequested)
		{
			cout << recordsPerPageCount << " records found this time through" << endl;
			cout << "Press any key to continue or 'x' or 'X' to break loop." << endl;
			int ichar = _getch();
			if (ichar == 'x' || ichar == 'X')
				BREAK;

			recordsPerPageCount = 0;
			recordMatched = false;
		 }

		recordsPerPageCount++;
    }

	infile.close();
The break statement I have in all caps is the issue (near the bottom of the code). I get a message saying that I can only use it to break out of a switch or loop, but since it is enclosed within the while loop's brackets, isn't that what it's doing? My confusion is heightened because what I have there is basically just copied off of an instructional video my teacher posted, and there is no error message on his.
Programming homework and newbie help thread Quote
06-16-2016 , 12:47 PM
What are you trying to break out of?
This?
if (recordMatched == true && recordsPerPageCount == recordsPerPageRequested)
Programming homework and newbie help thread Quote
06-16-2016 , 01:06 PM
Quote:
while (!infile.eof());
Errant semicolon is causing your loop to not be executed as expected.
Programming homework and newbie help thread Quote
06-16-2016 , 02:18 PM
Noodle Wazlib:

I was trying to break out of the larger while loop, the one with the errant semi colon.

PoppaTMan:

Lol, I'm an idiot. I can't believe how long I looked at that without finding it. Thank you.
Programming homework and newbie help thread Quote
06-17-2016 , 11:22 AM
So I'm almost done with code academies ruby track and decided to try out codewars, for a change of pace.

Second time I'm making what looks to be a working solution and I can't get it to pass. Any idea what is wrong?

Quote:
In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?
My code (now I know there is a very cool and short solution):

Code:
def makeNegative(num)
  if num < 0
    return num
  elsif num > 0
    return -num
  else
    return 0
  end
end
link to kata

Error message:
Quote:
Solution
should test for something
Expected: "expected", instead got: "actual"
"Your Test Cases" - still didn't figure out what it is.

Code:
# TODO: TDD development by writing your own tests as you solve the kata.
# These are some of the methods available:
#   Test.expect(boolean, [optional] message)
#   Test.assert_equals(actual, expected, [optional] message)
#   Test.assert_not_equals(actual, expected, [optional] message)

describe "Solution" do
  it "should test for something" do
    Test.assert_equals("actual", "expected")
  end
end

Last edited by RV-; 06-17-2016 at 11:46 AM. Reason: added Error message
Programming homework and newbie help thread Quote
06-17-2016 , 11:26 AM
maybe change
Code:
return -num
to
Code:
return num * -1
Don't think you can make variables negative in the way you're thinking

also

Code:
def makeNegative(num)
  if num < 0
    return num
  elsif num > 0
    return -num
  else
    return 0
  end
end
do you see a way to combine the bolded?
Programming homework and newbie help thread Quote
06-17-2016 , 11:32 AM
* -1 isn't working and when googling for turning positives into negatives I found that I can just put " - ". And by looking at other solutions it should work.

About the second part. I don't think I need to combine that(?). I need to return 0 if the num is zero. Forgot to mention that, my apologies.
Programming homework and newbie help thread Quote
06-17-2016 , 11:41 AM
What sort of thing is going wrong? Does it give any indication of what is failing?

Quote:
About the second part. I don't think I need to combine that(?). I need to return 0 if the num is zero.
Yes, if the number is zero, you return the number. Do you ever also just return the number without modifying it?

Edit

Also, looks like there's no test cases. Did you write your own? Otherwise maybe just try submitting yours instead of running tests.
Programming homework and newbie help thread Quote
06-17-2016 , 11:46 AM
Uh, I tried your code in codewars, and it worked fine.

What Noodle is getting at, though, is that you can consolidate your if statements and reduce the number of lines in your if/then/else. Think about it.
Programming homework and newbie help thread Quote
06-17-2016 , 11:47 AM
(P.S. you could easily do it as one line of code)
Programming homework and newbie help thread Quote
06-17-2016 , 11:49 AM
Edited my original post with more info. I got what Noodle is saying, 0 = 0 so else statement is not necessary.

Code:
def makeNegative(num)
  if num <= 0
    return num
  else 
    return -num
  end
end
Thank you, guys! Still have to figure out what is the deal with those test cases and what they are / how to use them.

Quote:
Originally Posted by RustyBrooks
(P.S. you could easily do it as one line of code)
Yes, I saw that and was pretty amused! Immediately googled ruby docs to understand it

Last edited by RV-; 06-17-2016 at 11:52 AM. Reason: reply to last RustyBooks post
Programming homework and newbie help thread Quote
06-17-2016 , 11:50 AM
Unless you wrote some test cases, there aren't any for that problem.
Programming homework and newbie help thread Quote
06-17-2016 , 11:52 AM
Which test cases are failing for you? I literally pasted your code in and it worked fine
Programming homework and newbie help thread Quote
06-17-2016 , 11:55 AM
rusty, try Run Tests instead of Submit. It'll fail. There's no default tests written for it.

RV,

if you want a one line solution using a ternary operator, check it:

Spoiler:
Code:
num <= 0 ? num : -num
Programming homework and newbie help thread Quote
06-17-2016 , 11:56 AM
Quote:
Originally Posted by RustyBrooks
Which test cases are failing for you? I literally pasted your code in and it worked fine
This is the one I'm seeing, but I have zero idea what the heck it is:

Quote:
# TODO: TDD development by writing your own tests as you solve the kata.
# These are some of the methods available:
# Test.expect(boolean, [optional] message)
# Test.assert_equals(actual, expected, [optional] message)
# Test.assert_not_equals(actual, expected, [optional] message)

describe "Solution" do
it "should test for something" do
Test.assert_equals("actual", "expected")
end
end
Quote:
Originally Posted by Noodle Wazlib
rusty, try Run Tests instead of Submit. It'll fail. There's no default tests written for it.

RV,

if you want a one line solution using a ternary operator, check it:

Spoiler:
Code:
num <= 0 ? num : -num
Nice one, thanks! I should memorize it. Having a difficulty remembering names for such things, let alone how the work.
Programming homework and newbie help thread Quote
06-17-2016 , 12:00 PM
Ahhh, so the Test Cases are like the extension of my Solution, i.e. I'm running my Test Case code on the Solution Code. Right?

I thought I should Test it before Submitting. Sorry for taking your time

Aaand now I feel stupid.
Programming homework and newbie help thread Quote
06-17-2016 , 12:02 PM
There's an even shorter solution than that! Although the ternary one is fine, imo.
Programming homework and newbie help thread Quote
06-17-2016 , 12:08 PM
I assume ruby has an absolute value function
Programming homework and newbie help thread Quote
06-17-2016 , 12:44 PM
so -Math.abs(num) ?
Programming homework and newbie help thread Quote
06-17-2016 , 01:09 PM
Quote:
Originally Posted by Noodle Wazlib
so -Math.abs(num) ?
or whatever the syntax is in ruby, yeah. I think it's num.abs
Programming homework and newbie help thread Quote
06-18-2016 , 05:36 AM
Quote:
Originally Posted by Noodle Wazlib
maybe change

Code:
def makeNegative(num)
  if num < 0
    return num
  elsif num > 0
    return -num
  else
    return 0
  end
end
do you see a way to combine the bolded?
ternary to the rescue!

Code:
num < 0 ? num : num > 0 ? num * -1 : 0
Cool thing is you can nest ternary expression to any level. As long as the conditions/expressions remain simple, it makes sense to write out an extend ternary expression.

Last edited by Craggoo; 06-18-2016 at 05:41 AM. Reason: keeping with how he structured the code. should have kept reading in the thread... oh well
Programming homework and newbie help thread Quote
06-18-2016 , 05:46 AM
expanding on previous post for a nested ternary... take this example...

Code:
if (x < 1) {
    if (x == 0) {
        return true
    }
} else if (x > 5) {
    if (x == 1) {
        return 1
    } else if (x == 2) {
        return 2
    } else {
        return false
    }
} else {
    return 'something else'
}
Assuming I made no mistakes, that above mess translated into a ternary looks like the following :

Code:
return x < 1 ? x == 0 ? true : void 0 : x > 5 ? x == 1 : 1 ? x == 2 ? 2 : false : 'something else'
Edit : to read nested ternarys is actually rather simple. If you have two consecutive expressions that end with a ? you are nesting the expression one level deeper. If you have two consecutive expressions that end with : you are breaking out of a nested ternary.

Last edited by Craggoo; 06-18-2016 at 05:59 AM.
Programming homework and newbie help thread Quote
06-18-2016 , 08:44 AM
Quote:
Originally Posted by Craggoo
ternary to the rescue!

Code:
num < 0 ? num : num > 0 ? num * -1 : 0
Cool thing is you can nest ternary expression to any level. As long as the conditions/expressions remain simple, it makes sense to write out an extend ternary expression.
Yay, let's make it less readable!

Either it is num or it is the negative of num.

Imo, nesting ternaries is worse than having a chain of if/elseif/elseif/.../else. Grab a switch instead.
Programming homework and newbie help thread Quote
Programming homework and newbie help thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Programming homework and newbie help thread

      
m