Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A data structure
#1
Hi all how would you write a function called manipulate_data which acts as follows:

When given a list of integers, return a list, where the
first element is the count of positives numbers
and the
second element is the sum of negative numbers
.
NB: Treat 0 as positive.
Reply
#2
(Nov-11-2016, 08:49 AM)akulamartin Wrote: Hi all how would you write a function called manipulate_data which acts as follows:

When given a list of integers, return a list, where the
first element is the count of positives numbers
and the
second element is the sum of negative numbers
.
NB: Treat 0 as positive.

What have you tried? Show us your best attempt and ask specific questions regarding possible problems/errors.
Reply
#3
def manipulate_data(list):
 for num in range(list):
   if list.item()<0:
   
   if list.item()>0:
Reply
#4
how would i write it?

as a module!

but i probably would code the function and caller in the same file to make testing go smoother since this is a very complicated data structure.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
akulamartin, we're happy to help you learn Python but we're not going to do your homework for you. When you post here, you should ask your question, not copy-paste your homework prompt.

In this case, you need to provide your code (with any input hard-coded to reproduce the problem), your expected output, and your actual output, which may be an error message (which should be included in full), along with a natural language explanation of what you think it should work when it doesn't, or a question other than "why doesn't this work?" There are loads of resources if you're not quit there yet.
Reply
#6
(Nov-11-2016, 09:12 AM)akulamartin Wrote:
def manipulate_data(list):
 for num in range(list):
   if list.item()<0:
   
   if list.item()>0:

1) don't call your variable "list", that's a builtin function that you shouldn't overwrite.
2) why are you using range()?  for num in list works fine.
3) you'll get an error without a body in the if blocks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list, map and put of the Queue in the Tree Data Structure longmen 6 1,664 Mar-30-2022, 11:09 PM
Last Post: longmen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020