Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Wee Question

Options
  • 16-07-2007 2:38pm
    #1
    Closed Accounts Posts: 1,981 ✭✭✭


    Psuedo-code or in java or c++ to take an int array of letters and give you the max consecutive occurence of those letters in a row. (assume theres only 2 letters, a and b)

    e.g int array = aaabbbbbbaabbbaaabbb

    In this case the max would be 6 since we have 6 b's in a consecuative row.

    Anyone give this a shot ?


Comments

  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    How about you show what you've tried so far?


  • Registered Users Posts: 6,790 ✭✭✭cornbb


    How can your array hold characters if you're declaring int as the datatype?


  • Registered Users Posts: 85 ✭✭slavigo


    
    # input list of characters
    all_chars = 'aaabbbbbbaabbbaaabbb'
    
    # placeholder for current character and it's count
    current_char = ''
    current_count = 0
    
    # placeholder for biggest occurrence of a character so far
    # and it's count
    biggest_char = ''
    biggest_count = 0
    
    # for each character
    for char in all_chars:
            # If first character or a different character to the
            # character we are currently counting
            if not current_char or not char == current_char:
                    # If we've met a new character and the last run of chars
                    # was longer than the previously recorded longest run
                    if current_count > biggest_count:
                            # Save both character and count
                            biggest_char = current_char
                            biggest_count = current_count
                    # Save the new character and start a new occurrences count
                    current_char = char
                    current_count = 1
            # Else it's the same character again
            else:
                    # So increment our current running total
                    current_count = current_count + 1
    # if count of occurrences at the end of the input list of characters
    # is the longest count for a character. Save it.
    if current_count > biggest_count:
            biggest_char = current_char
            biggest_count = current_count
    
    # Print out our results.
    print biggest_char
    print biggest_count
    
    

    Here you go. It's written in python. Kinda like pseudo code I suppose but if you've got python, a little bit of copy and paste will have it running. Oh, one thing this example doesn't allow for is a case where two counts of the biggest occurrences are the same. I.e. if a and b both had 6 occurrences. This program would return the first character of that length that it met.

    Hope this helps.


  • Registered Users Posts: 221 ✭✭Elfman


    hmm top of my head :

    while(array.hasanotherelement()){


    if (array[cnt] == array[cnt+1]){
    concnter++

    }
    else{
    if(currentlrgnumber < concnter){

    currentlrgnumber = concnter;
    }//if

    concntere = 0;

    }//else


    }//while

    system.out.println("largest num is " + currentlrgnumber);


    this is a rough guide to the logic. there some itialisatioin to be done but sin e.


  • Registered Users Posts: 85 ✭✭slavigo


    Cake Fiend wrote:
    How about you show what you've tried so far?

    Sorry Cake Fiend, Just read this:
    "Homework & Learning to code: How to ask a question! (The Rules)"
    I see where you were going with your request now.
    Which of coarse was in the right direction.

    My bad but it won't happen again.


  • Advertisement
  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    cornbb wrote:
    How can your array hold characters if you're declaring int as the datatype?

    Characters are stored in memory as ascii values (i.e. small integers)


Advertisement