0% found this document useful (0 votes)
143 views1 page

Lab 4.4 Number Cruncher

This document provides instructions for writing custom blocks in SNAP to process and transform lists of numbers. It asks the reader to write blocks to calculate the sum, average, maximum value of a list, check if a list contains negatives, and check if numbers in a list are increasing. It also asks them to write blocks to make all numbers positive, filter a list to only even numbers, and add corresponding elements of two lists.

Uploaded by

Cora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views1 page

Lab 4.4 Number Cruncher

This document provides instructions for writing custom blocks in SNAP to process and transform lists of numbers. It asks the reader to write blocks to calculate the sum, average, maximum value of a list, check if a list contains negatives, and check if numbers in a list are increasing. It also asks them to write blocks to make all numbers positive, filter a list to only even numbers, and add corresponding elements of two lists.

Uploaded by

Cora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Introduction

 to  Computer  Science  

Number  Cruncher  
In  this  lab,  you  will  continue  practicing  processing  lists,  this  time  using  lists  of  numbers  instead  of  words.  

Summarizing  Numbers  
1. Write  a  custom  SNAP  reporter  block  called  "sum"  that  takes  a  list  as  an  argument  and  reports  the  sum  
of  all  the  numbers  in  the  list.  You  can  assume  that  all  items  in  the  list  passed  as  the  argument  will  be  
numbers,  though  you  should  not  assume  anything  else.  

2. Write  a  custom  SNAP  reporter  block  called  "average"  that  takes  a  list  as  an  argument  and  reports  the  
average  of  all  the  numbers  in  the  list.  As  above,  you  can  assume  that  all  items  in  the  list  passed  as  the  
argument  will  be  numbers,  but  you  should  not  assume  anything  else.  

3. Write  a  custom  SNAP  predicate  block  called  "includes  negative"  that  takes  a  list  as  an  argument  and  
reports  true  if  the  list  contains  at  least  one  negative  number,  and  false  if  all  numbers  are  non-­‐negative.  

4. Write  a  custom  SNAP  predicate  block  called  "increasing?"  that  takes  a  list  of  numbers  as  an  argument  
and  reports  true  if  each  value  in  the  list  is  greater  than  or  equal  to  the  one  before  it.  

5. Write  a  custom  SNAP  reporter  block  called  "maximum"  that  takes  a  list  as  an  argument  and  reports  the  
largest  number  in  the  list.  

Transforming  Lists  
1. Write  a  custom  SNAP  reporter  block  called  "make  all  positive"  that  takes  a  list  of  numbers  as  an  
argument  and  reports  a  new  list  that  is  the  same  as  the  argument,  except  all  negative  numbers  have  
been  replaced  by  their  absolute  value.  

2. Write  a  custom  SNAP  reporter  block  called  "only  evens"  that  takes  a  list  of  integers  as  an  argument  and  
reports  a  new  list  that  contains  only  the  even  numbers  from  the  argument  list.  The  result  list  should  
have  its  values  in  the  same  order  as  the  original  list,  but  with  the  odd  integers  removed.  (Remember  
that  "mod"  block  can  be  useful  in  determining  whether  or  not  a  number  is  even.)  

3. BONUS:  Write  a  custom  SNAP  reporter  block  called  "add  all"  that  takes  two  list  of  numbers  as  
arguments  and  returns  a  new  list  that  contains  the  sum  of  the  corresponding  values  in  each  argument  
list.  For  example,  if  the  arguments  to  "add  all"  are  (1,  4,  6)  and  (2,  2,  3),  the  result  should  be  (3,  6,  9).  
You  can  assume  the  two  lists  will  be  the  same  size.    

 
This  work  is  licensed  under  a  Creative  Commons  Attribution-­‐
NonCommercial-­‐ShareAlike  4.0  International  License    

You might also like