-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
solution2.5.2.8.scala provided #76
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
94c5af7
solution2.5.2.8.scala provided
andreas-roehler 7e063ff
solution2.5.2.8_stream_iterate.scala provided
andreas-roehler 2169e5f
Use revDigits in isPalindrome
andreas-roehler 216a701
Use revDigits in isPalindrome again
andreas-roehler 5e88690
typo
andreas-roehler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
Exercise 2.5.2.8 | ||
|
||
(a) Reverse an integer’s digits (see Example 2.5.1.6) as shown: | ||
def revDigits(n: Int): Int = ??? | ||
|
||
scala> revDigits(12345) | ||
res0: Int = 54321 | ||
|
||
(b) A palindrome integer is an integer number n such that revDigits(n) == n. | ||
|
||
Write a predicate function of type Int => Boolean that checks | ||
whether a given positive integer is a palindrome. | ||
*/ | ||
|
||
def revDigits (n: Int): Int = { | ||
n.toString.reverse.toInt | ||
} | ||
|
||
val result = revDigits(12345) | ||
val expected: Int = 54321 | ||
assert(result == expected) | ||
|
||
def isPalindrome(n: Int): Boolean = { | ||
n.toString.reverse.toInt == n | ||
} | ||
|
||
val iPexpected: Boolean = false | ||
val iPresult = isPalindrome(123) | ||
assert(iPresult == iPexpected) | ||
|
||
val iPexpected2: Boolean = true | ||
val iPresult2 = isPalindrome(123321) | ||
assert(iPresult2 == iPexpected2) | ||
|
||
// scala> :load solution2.5.2.8.scala | ||
// :load solution2.5.2.8.scala | ||
// def revDigits(n: Int): Int | ||
// val result: Int = 54321 | ||
// val expected: Int = 54321 | ||
// def isPalindrome(n: Int): Boolean | ||
// val iPexpected: Boolean = false | ||
// val iPresult: Boolean = false | ||
// val iPexpected2: Boolean = true | ||
// val iPresult2: Boolean = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
Exercise 2.5.2.8 | ||
|
||
(a) Reverse an integer’s digits (see Example 2.5.1.6) as shown: | ||
def revDigits(n: Int): Int = ??? | ||
|
||
scala> revDigits(12345) | ||
res0: Int = 54321 | ||
|
||
(b) A palindrome integer is an integer number n such that revDigits(n) == n. | ||
|
||
Write a predicate function of type Int => Boolean that checks | ||
whether a given positive integer is a palindrome. | ||
*/ | ||
|
||
def revDigits(n: Int): Int = { | ||
Stream.iterate(n)(_ / 10).takeWhile(_ != 0).map(_ % 10).toList.reverse.foldRight(0)(_ + _ * 10) | ||
} | ||
|
||
val result = revDigits(12345) | ||
val expected: Int = 54321 | ||
assert(result == expected) | ||
|
||
def isPalindrome(n: Int): Boolean = { | ||
n.toString.reverse.toInt == n | ||
} | ||
|
||
val iPexpected: Boolean = false | ||
val iPresult = isPalindrome(123) | ||
assert(iPresult == iPexpected) | ||
|
||
val iPexpected2: Boolean = true | ||
val iPresult2 = isPalindrome(123321) | ||
assert(iPresult2 == iPexpected2) | ||
|
||
// scala> :load solution2.5.2.8_stream_iterate.scala | ||
// :load solution2.5.2.8_stream_iterate.scala | ||
// there was 1 deprecation warning; re-run with -deprecation for details | ||
// 1 warning found | ||
// def revDigits(n: Int): Int | ||
// val result: Int = 54321 | ||
// val expected: Int = 54321 | ||
// def isPalindrome(n: Int): Boolean | ||
// val iPexpected: Boolean = false | ||
// val iPresult: Boolean = false | ||
// val iPexpected2: Boolean = true | ||
// val iPresult2: Boolean = true |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, I would expect you to call
revDigits
to get the reversed digits, rather thantoString
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'm on it.