3. How many 3 digit numbers are divisible by 17? Write a function to print them and return the sum. (The sum being the sum of the 3 digit numbers that are divisible by 17).

4. Write a program that does the following to an image:
changes black pixels to white and white pixels to black
for non-black and non-white pixels, do the following:
reduce the red by 50% if the value of red is above 200
reduce the red by 20% if the value of red is between 100 (inclusive) and 200 (inclusive)
increase the red by 50% if the value of red is below 100
return the total number of pixels increased or decreased by 50% (not including the black & white pixels)
How did you test your program? How do you know that it works? Show a list of test cases that will ensure that every branch of the program is followed.

Using Jython Envirenment for Student


the Q3: I dont know how to sum the answer
Q4, no idea

#===============E 3========================

# This allow any number you want
def dvsble(x,y):  
   for n in range(x,y): 
     sum = 0   
     if n%17 ==0:
      x = n
      sum()
      print n       
     else:      
      n=n+1     
   print sum

Q4:

def color():
  pic = makePicture(pickAFile())
  for x in range(getWidth(pic)):
    for y in range(getHeight(pic)):
      px = getPixel(pic,x,y)
      color = getColor(px)
      if getColor(Color(255,255,255)):       
         setColor(color,Color(0,0,0))
      elif  getColor(0,0,0):
            getColor(255,255,255)
  show(pic)

Dani AI

Generated

As already found, collecting the three‑digit multiples of 17 is straightforward. A slightly cleaner, constant‑space approach uses the arithmetic progression: the first 3‑digit multiple is 102 and the last is 986, so there are 53 values and their sum is 28832 (sum = 53*(102+986)/2). A concise Python expression that avoids making a full list is sum(range(102, 987, 17)). Also follow 's advice: avoid shadowing built‑ins (do not use sum as a variable name).

For Q4 the logic and test strategy are the important parts. Process every pixel once, handling these cases in order:

  • If the pixel is pure black (0,0,0) set it to pure white (255,255,255).
  • Else if pure white set it to black.
  • Else read the original red component r and:
    • if r > 200: set r = int(r * 0.5) and increment the 50% counter;
    • elif 100 <= r <= 200: set r = int(r * 0.8) (do NOT count);
    • else (r < 100): set r = int(r * 1.5) and increment the 50% counter.
      After computing r clamp it to 0..255, preserve green and blue, and write the new color back. Important implementation notes: compare component values (not whole Color objects) in JES, evaluate and store the original r before changing it, convert to integers, and always clamp.

Example implementation (Pillow) — this is easy to test and shows the required behavior:

from PIL import Image

def transform_image(in_path, out_path):
    im = Image.open(in_path).convert('RGB')
    pixels = im.load()
    w, h = im.size
    count50 = 0
    for x in range(w):
        for y in range(h):
            r, g, b = pixels[x, y]
            if (r, g, b) == (0, 0, 0):
                pixels[x, y] = (255, 255, 255)
            elif (r, g, b) == (255, 255, 255):
                pixels[x, y] = (0, 0, 0)
            else:
                if r > 200:
                    r = int(r * 0.5); count50 += 1
                elif 100 <= r <= 200:
                    r = int(r * 0.8)
                else:
                    r = int(r * 1.5); count50 += 1
                r = max(0, min(255, r))
                pixels[x, y] = (r, g, b)
    im.save(out_path)
    return count50

Minimal test cases to cover every branch: pixels with (0,0,0), (255,255,255), (201,50,50), (200,50,50), (100,50,50), (99,50,50). For JES, adapt pixel read/write to its pixel accessors and compare components rather than Color objects.

Recommended Answers

All 3 Replies

#===============E 3========================

# This allow any number you want
def dvsble(x,y):  
   for n in range(x,y): 
     sum = 0   
     if n%17 ==0:
      x = n
      sum()
      print n       
     else:      
      n=n+1     
   print sum

Some comments to help going forward:

  1. Are the parameter choice correct considering restrictions of this request
  2. Line 5 task said range of three number integers, what is smallest one and what biggest?
  3. what happens on line at beginning of loop at line 6, is it what you want
  4. where is x used?
  5. line 9: value of sum is 0 so you are using 0 as function without parameters
  6. lines 10 and 13 are printing values of n and the address of built in sum, which you set 0 at line 0
  7. There is contradiction between line 5 and 12 both advancing the value of n

I would suggest to collect all correct numbers to list and returning them from the function if you learned about lists allready.
I would not replace the builtin function sum with integer even locally in function. It is efficient way of doing sum at one go from collected numbers (this task is actually one line expression with experienced coders with generator expression)

def dvsble():  
# use the sum in list
   l = [n for n in range(100,1000) if n%17 == 0]
   print l
   return sum(l)

I know how to do Q3

But still no idea for Q4

code for Q4? So we help you to finish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.