Programming Challenges

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
30,589
Hi,

So I'm looking for some practice stuff to learn new concepts and expand my horizons.
Was using this : https://www.reddit.com/r/dailyprogrammer

But the challenges for this week are a little out there :wtf:

Any sites you can recommend on challenges to test your know how?
The whole, look at a problem you have and solve it isn't working because I'm kinda perfect :p
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,887
One I had for an interview some time back.

Question:

Write a function that checks if a given sentence is a palindrome. A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. Only the order of English alphabet letters (A-Z and a-z) should be considered, other characters should be ignored.

For example, IsPalindrome("Noel sees Leon.") should return true as spaces, period, and case should be ignored resulting with "noelseesleon" which is a palindrome since it reads same backward and forward.

My Answer:

Code:
using System;

public class Palindrome
    {
        public static bool IsPalindrome(string str)
        {

            int min = 0;
            int max = str.Length - 1;
            while (true)
            {
                if (min > max)
                {
                    return true;
                }
                char a = str[min];
                char b = str[max];

                // Scan forward for a while invalid.
                while (!char.IsLetterOrDigit(a))
                {
                    min++;
                    if (min > max)
                    {
                        return true;
                    }
                    a = str[min];
                }

                // Scan backward for b while invalid.
                while (!char.IsLetterOrDigit(b))
                {
                    max--;
                    if (min > max)
                    {
                        return true;
                    }
                    b = str[max];
                }

                if (char.ToLower(a) != char.ToLower(b))
                {
                    return false;
                }
                min++;
                max--;
            }



            throw new NotImplementedException("Waiting to be implemented.");
        }

        public static void Main(string[] args)
        {
            Console.WriteLine(IsPalindrome("Noel sees Leon."));
            Console.ReadLine(); 
        }
    }
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
One I had for an interview some time back.
Cool, but you could have also done it with an array: ToCharArray() and Linq (Select & Where)

Here e.g. is the same in Swift; using an array and higher order functions (i.e. similar concept to Linq).
Code:
extension String
{
  func isPalindrome() -> Bool {
    let lower = self.lowercaseString.unicodeScalars.filter
      { 97...122 ~= Int($0.value) }.map { String($0) }.joinWithSeparator("")
    return lower == String(lower.characters.reverse())
  }
}

"Noel sees Leon.".isPalindrome() // true

C# Linq version example, hopefully code correct; didn't bother to run it through VS.
Code:
public static bool IsPalindrome(string str)
{
   var onlyLetters = str.ToLower().Where(c => Char.IsLetter(c)).ToArray();
   return new String(onlyLetters) == new String(onlyLetters.Reverse());
}
 
Last edited:

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
30,589
One I had for an interview some time back.

Question:



My Answer:

Code:
using System;

public class Palindrome
    {
        public static bool IsPalindrome(string str)
        {

            int min = 0;
            int max = str.Length - 1;
            while (true)
            {
                if (min > max)
                {
                    return true;
                }
                char a = str[min];
                char b = str[max];

                // Scan forward for a while invalid.
                while (!char.IsLetterOrDigit(a))
                {
                    min++;
                    if (min > max)
                    {
                        return true;
                    }
                    a = str[min];
                }

                // Scan backward for b while invalid.
                while (!char.IsLetterOrDigit(b))
                {
                    max--;
                    if (min > max)
                    {
                        return true;
                    }
                    b = str[max];
                }

                if (char.ToLower(a) != char.ToLower(b))
                {
                    return false;
                }
                min++;
                max--;
            }



            throw new NotImplementedException("Waiting to be implemented.");
        }

        public static void Main(string[] args)
        {
            Console.WriteLine(IsPalindrome("Noel sees Leon."));
            Console.ReadLine(); 
        }
    }

Comparing strings with a != doesn't look right. Why not string.compare() ?
 

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
30,589
[)roi(];17687068 said:
Cool, but you could have also done it with an array: ToCharArray() and Linq (Select & Where)

Here e.g. is the same in Swift; using an array and higher order functions (i.e. similar concept to Linq).
Code:
extension String
{
  func isPalindrome() -> Bool {
    let lower = self.lowercaseString.unicodeScalars.filter
      { 97...122 ~= Int($0.value) }.map { String($0) }.joinWithSeparator("")
    return lower == String(lower.characters.reverse())
  }
}

"Noel sees Leon.".isPalindrome() // true

Find using the ToCharArray is the best one.
Maybe you guys can help me with the Digital root. Will post my sad code in a bit.

Code:
static int AddDigits(int num)
        {
            int val = 0;
            char[] arr = (num.ToString()).ToCharArray();
 
            if (arr.Length != 1)
            {
                foreach (var i in arr)
                {
                    val = val + int.Parse(i.ToString());
                }
                if (arr.Length != 1)
                    return AddDigits(val);
            }
            return val;
 
 
        }
 
Last edited:

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Comparing strings with a != doesn't look right. Why not string.compare() ?
You should review Microsoft's recommendations:
Use an overload of the String.Equals method to test whether two strings are equal.
Use the String.Compare and String.CompareTo methods to sort strings, not to check for equality.
== for String maps through to public static bool Equals(string a, string b) re String is a reference type that behaves like a value type.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Find using the ToCharArray is the best one.
Maybe you guys can help me with the Digital root. Will post my sad code in a bit.

Code:
static int AddDigits(int num)
        {
            int val = 0;
            char[] arr = (num.ToString()).ToCharArray();
 
            if (arr.Length != 1)
            {
                foreach (var i in arr)
                {
                    val = val + int.Parse(i.ToString());
                }
                if (arr.Length != 1)
                    return AddDigits(val);
            }
            return val;
 
 
        }
What the context for the AddDigits method? Add the digits in a String? How about an example?
 

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
30,589
[)roi(];17687230 said:
What the context for the AddDigits method? Add the digits in a String? How about an example?

Basically you get a number. 64 for example. For each digit in the number parsed in, you need to add them ie, 6 + 4 until you only get 1 digit remaining.
It's digital root is :
6+4 = 10
--> 1+ 0 = 1

1 is the only digit so it is the root.

First part is figuring out how to do this in recursion. Second is doing it without recursion.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Basically you get a number. 64 for example. For each digit in the number parsed in, you need to add them ie, 6 + 4 until you only get 1 digit remaining.
It's digital root is :
6+4 = 10
--> 1+ 0 = 1

1 is the only digit so it is the root.

First part is figuring out how to do this in recursion. Second is doing it without recursion.
Is this a parser for string calculator? i.e. given the following string "1 + 4 - 3" you return 2?
Or is this to simply add the successive digits in a given string "123" to return 6?
 
Top