patterncsharpMinor
Number palindrome checker
Viewed 0 times
palindromenumberchecker
Problem
My daughter, nine, recently had some homework looking at number palindromes. Part of the assignment was to take a number, reverse it, add the two together and repeat until she got a palindrome and to do this for several different numbers.
While helping her with this it struck me it'd be a nice simple idea for a computer program. She & I have messed about in Scratch a bit, but this sounded like a nice problem to do in a more commercial language. Thought I could write it and then show her how it worked.
So I did. It turned out to be not quite as child-friendly as I'd imagined.
```
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace CheckNumberPalindrome
{
class Program
{
static void Main(string[] args)
{
string rollingSum;
int iterations = 1;
if(IsNumeric(args[0]) == false)
{
Console.WriteLine("You did not enter a numeric value");
}
rollingSum = RecurseUntilPalindrome(args[0], ref iterations);
Console.WriteLine(string.Format("The final palindrome is {0}, which took {1} steps.", rollingSum, iterations));
}
private static bool IsNumeric(string s)
{
long result;
return Int64.TryParse(s, out result);
}
private static string RecurseUntilPalindrome(string s, ref int iterations)
{
long rollingSum = AddToPalindrome(s);
if(IsPalindrome(rollingSum.ToString()))
{
return rollingSum.ToString();
}
iterations++;
return RecurseUntilPalindrome(rollingSum.ToString(), ref iterations);
}
private static long AddToPalindrome(string s)
{
string reversedString = ReverseString(s);
long result = Convert.ToInt64(s) + Convert.ToInt64(reversedString);
Console.WriteLine(string.Format("Adding {0} to {1} = {
While helping her with this it struck me it'd be a nice simple idea for a computer program. She & I have messed about in Scratch a bit, but this sounded like a nice problem to do in a more commercial language. Thought I could write it and then show her how it worked.
So I did. It turned out to be not quite as child-friendly as I'd imagined.
```
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace CheckNumberPalindrome
{
class Program
{
static void Main(string[] args)
{
string rollingSum;
int iterations = 1;
if(IsNumeric(args[0]) == false)
{
Console.WriteLine("You did not enter a numeric value");
}
rollingSum = RecurseUntilPalindrome(args[0], ref iterations);
Console.WriteLine(string.Format("The final palindrome is {0}, which took {1} steps.", rollingSum, iterations));
}
private static bool IsNumeric(string s)
{
long result;
return Int64.TryParse(s, out result);
}
private static string RecurseUntilPalindrome(string s, ref int iterations)
{
long rollingSum = AddToPalindrome(s);
if(IsPalindrome(rollingSum.ToString()))
{
return rollingSum.ToString();
}
iterations++;
return RecurseUntilPalindrome(rollingSum.ToString(), ref iterations);
}
private static long AddToPalindrome(string s)
{
string reversedString = ReverseString(s);
long result = Convert.ToInt64(s) + Convert.ToInt64(reversedString);
Console.WriteLine(string.Format("Adding {0} to {1} = {
Solution
This may help with your second point about code concepts:
How about addressing the special case for when the user enters a palindrome? It looks to me like you are assuming the entered number will not be a palindrome and you automatically add to it (calling RecurseUntilPalindrome). How about adding an initial check to account for that case? I think it would go along well with your numerical value input check (I consider invalid user input as a sort of special case as well).
To address your first point, I think your code looks pretty clean as is! Really easy to follow.
How about addressing the special case for when the user enters a palindrome? It looks to me like you are assuming the entered number will not be a palindrome and you automatically add to it (calling RecurseUntilPalindrome). How about adding an initial check to account for that case? I think it would go along well with your numerical value input check (I consider invalid user input as a sort of special case as well).
To address your first point, I think your code looks pretty clean as is! Really easy to follow.
Context
StackExchange Code Review Q#84209, answer score: 3
Revisions (0)
No revisions yet.