Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Palindrom C#

Discussion in 'General Discussion' started by Joggla, Jun 16, 2023.

Thread Status:
Not open for further replies.
  1. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    I know this is a Unity Forum but I like this community and think you guys could help me.
    So because I keep getting stuck on my projects I thought I would try an old "easy" coding problem just to see if I have grown.

    This is my solution:

    Code (CSharp):
    1. namespace Palindrom
    2. {
    3.     internal class Program
    4.     {
    5.         static void Main(string[] args)
    6.         {
    7.             Console.WriteLine("Type in word!");
    8.             string word = Console.ReadLine();
    9.             bool isPalindrom = false;
    10.             for(int i = 0; i <= word.Length - 1; i++)
    11.             {
    12.                 if (word[i] == word[word.Length -1 -i])
    13.                 {
    14.                     isPalindrom = true;
    15.                     if(i >= word.Length/2)
    16.                     {
    17.                         break;
    18.                     }
    19.                 } else
    20.                 {
    21.                     isPalindrom = false;
    22.                     break;
    23.                 }
    24.             }
    25.             Console.WriteLine(isPalindrom);
    26.         }
    27.     }
    28. }
    Is it good or bad? I know this is something very simple but I needed to complete one task once again to maby feel better. Any advice?
     
    Last edited: Jun 16, 2023
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,734
    This is general forum, not support forum.
    You are member since 2019 and should know by now, that you should ask such questions in scripting sub forum.

    Please do not abuse general sub forum for this kind of questions.
     
  3. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    I posted it in the scripting forum, but someone moved it to here :(
     
    Antypodish likes this.
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,498
    You're writing loop by hand instead of using library functions. I'm not a fan of this approach.

    For example:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Linq;
    4.  
    5. public class Program
    6. {
    7.     public static bool isPalindrome(string s){
    8.         return s == new string(s.Reverse().ToArray());
    9.     }
    10.   ...
    11. }
    12.  
     
    Joggla likes this.
  5. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    where and why are you getting stuck in your projects?

    what does this script have to do with it?
     
  6. marce155

    marce155

    Joined:
    Jun 8, 2014
    Posts:
    8
    Oh I enjoy these!

    How about this:

    Code (CSharp):
    1. static bool IsPalindrome(ReadOnlySpan<char> strToCheck)
    2. {
    3.     var upperBound = strToCheck.Length / 2;
    4.     for (var i = 0; i < upperBound; i++)
    5.     {
    6.         if (strToCheck[i] != strToCheck[^(i + 1)])
    7.         {
    8.             return false;
    9.         }
    10.     }
    11.  
    12.     return true;
    13. }
    Edit: removed redundant code, it's too late for my brain :D
     
    Last edited: Jun 16, 2023
  7. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    I just thought ill go through an exercise beginners go through to see if I can do them.
    Even on simple projects like the towerdefense game I am working on, suddenly has close to 100 scripts, all doing stuff and it gets hard remembering all that each script does and how they interact with eachother.
     
  8. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181

    Ah I see.

    You might find it worthwhile to look into studying and practicing a few code design patterns. I like this website a lot for a quick reference:
    https://refactoring.guru/design-patterns

    though for unity specific implementation it helps a lot to bring specific problems here. Much of the instruction you find for design patterns sounds way more complicated than it is if you only read about it. some people here may be able to show easier to understand examples related to your own problems, or clear up any confusion you are left with after reading tutorials.

    Jason Weisman (sp?) has a few examples on youtube as well.

    the idea of design patterns is to solve exactly this problem that you described - how can a person manage a big complex system? in short, by having a standardized pattern which answers what sort of classes you create and how they communicate makes it so that you don't have to remember so many specific things, but rather just a few general things.
     
  9. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,498
    This is a higher level task than palindrome algorithm programming. Basically, you won't learn how to keep large body of code maintainable and easy to understand by implementing basic beginner algorithm. To get better at this, you'd need to, well, write larger programs and figuring out how to organize them.
     
  10. UhOhItsMoving

    UhOhItsMoving

    Joined:
    May 25, 2022
    Posts:
    101
    Your loop is overcomplicated (ex. you have two loop conditions: one outside and one inside the loop); it can be done in one line. Also, an optimization you can do is replace Length - 1 - i with Length - ++i since those are equal; this moves the iterator to inside the loop.
    Code (CSharp):
    1. bool isPalindrom = word.Length > 0;
    2. for(int i = 0; i < word.Length / 2;) if (word[i] != word[word.Length - ++i]) { isPalindrom = false; break; }
     
Thread Status:
Not open for further replies.