Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:03:24+00:00 2026-06-04T02:03:24+00:00

i was helping my friend to do logic algorithm in python but i haven’t

  • 0

i was helping my friend to do logic algorithm in python but i haven’t come with best solution yet.

first of all, i have a list of array:

x = array[0,1,2,3,4,3,2,3,-2,-4,-7,2,2]

and he wanted to categorize the x so the output become like this:

array([0,1,2,3,4]) # increasing value
array([4,3,2]) #decreasing value
array([2,3]) # increasing value
array([3,-2,-4,-7]) #decreasing value
array([-7,2]) # increasing value
array([2,2])  # remain_the_same_value

the rule is simple:

  1. if the value keep increasing (like example above: 0,1,2,3,4) they put in one array
  2. if the value keep decreasing (like example above: 3,-2,-4,-7) they put in one array
  3. but, if there is a sudden change in value pattern such as example above: from the increasing value (0,1,2,3,4) suddenly the next value is decreasing. the new array will be made and put the last increasing value which is (4) and monitor the next value, whether it is decreasing value or not. If yes, they will be put in one array. example :array([4,3,2])
  4. if the the value is remain the same (like example above, from 2 to 2). they will be put in one array.

this is what i come so far, but still far from the solution

#categorize which types of input
if len(x) > 2 :
    for i in range(len(x)) :
        if (x[i+1]-x[i]) > 0 and i+i < len(x) : # for increasing x value

        elif (x[i+1]-x[i]) < 0 and i+i < len(x) : # for decreasing x value

        elif (x[i+1]-x[i]) == 0 and i+i < len(x) : # for foward direction of vehicle

        else :
            print 'ERROR : check the input coordinates once again!'

best regards,

Glenn

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:03:25+00:00Added an answer on June 4, 2026 at 2:03 am

    Firstly I would like to say that I don’t understand one part of your question,

    array([3,-2]) #decreasing value
    array([-2,-4,-7]) #decreasing value
    

    Why are these separate?

    I will post my answer so far which gives the correct results except for that section since i don’t see the logic behind it. This example uses lists and tuples for simplicity but you can change it to use array if you want.

    >>> from itertools import groupby
    >>> data = [0,1,2,3,4,3,2,3,-2,-4,-7,2,2]
    >>> def slope(a,b): #Returns 1 for inc, 0 for equal and -1 for dec
            return (a > b) - (a < b) 
    
    >>> def groups(nums):
            for k,v in groupby(zip(nums,nums[1:]), lambda (x,y): slope(x,y)):
                yield next(v) + tuple(y for x,y in v) #Using itertools.chain this can be written as tuple(chain(next(v),(y for x,y in v)))
    
    
    >>> list(groups(data))
    [(0, 1, 2, 3, 4), (4, 3, 2), (2, 3), (3, -2, -4, -7), (-7, 2), (2, 2)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm helping a friend work through validating a site, but at the second im
I am helping a friend with his C# homework, but is being a long
I am helping a friend from first year to prepare his exam on Java.
I'm helping a friend create an android app that will have screens with lists
I have a strange whitespace on the webpage I'm helping design for my friend
I have been helping someone debug some code where the error message was Day
Thank you all for helping. Below this post I put the corrected version's of
I was helping a friend to write some Java code, who doesn't know a
I'm helping a friend with a java problem. However, we've hit a snag. We're
I am helping a friend to turn her weekly comic strip into an iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.