Difference between revisions of "Python:Accumulate Checking an Array"

From Progzoo
Jump to: navigation, search
(Created page with "{{Accumulator Tutorial}} You can use an accumulator to check if some condition is true for every element. For example we can check that every element is equal to 10. The array...")
 
Line 34: Line 34:
  
 
<prog>
 
<prog>
for v in a:
+
def f(a):
  if v!=10:
+
  for v in a:
    return False
+
    if v!=10:
return True
+
      return False
 +
  return True
 
</prog>
 
</prog>
 
<answer>
 
<answer>
 +
def f(a):
 
  for v in a:
 
  for v in a:
 
   if v!=1:
 
   if v!=1:
Line 51: Line 53:
 
<p class='imper'>Return true if '''1''' occurs in the array, otherwise return false.</p>
 
<p class='imper'>Return true if '''1''' occurs in the array, otherwise return false.</p>
 
<prog>
 
<prog>
for v in a:
+
def f(a):
  if v!=10:
+
  for v in a:
    return False
+
    if v!=10:
return True
+
      return False
 +
  return True
 
</prog>
 
</prog>
 
<answer>
 
<answer>
for v in a:
+
def f(a):
  if v==1:
+
  for v in a:
    return True
+
    if v==1:
return False
+
      return True
 +
  return False
 
</answer>
 
</answer>
 
</question>
 
</question>
Line 86: Line 90:
 
<hint tease="s.startsWith()">
 
<hint tease="s.startsWith()">
 
You can use the string method startsWith
 
You can use the string method startsWith
  if (s.startswith("s"))
+
  if (s.startswith("s")):
 
</hint>
 
</hint>
 
<prog>
 
<prog>
for v in a:
+
def f(a):
  if v!="s":
+
  for v in a:
    return False
+
    if v!="s":
return True
+
      return False
 +
  return True
 
</prog>
 
</prog>
<answer lang="java"><![CDATA[
+
<answer><![CDATA[
for v in a:
+
def f(a):
  if not v.startswith("s"):
+
  for v in a:
    return True
+
    if not v.startswith("s"):
return False
+
      return True
 +
  return False
 
]]></answer>
 
]]></answer>
 
</question>
 
</question>
 
{{Accumulator Tutorial}}
 
{{Accumulator Tutorial}}

Revision as of 16:47, 12 July 2017

You can use an accumulator to check if some condition is true for every element. For example we can check that every element is equal to 10. The array {10, 10, 10} would pass the test and return true, the array {10, 12, 10} would fail the test and return false.

acc = True;
for v in a:
  if v!=10:
    acc = False
return acc

We can do a little better than that. As soon as we find one value that is false there is no need to look at the rest of the array. We can "short circuit" the evaluation by returning immediately we find a false.

for v in a:
  if v!=10:
    return False
return True


All 1s

Return true if every element is equal to 1, otherwise return false.



[Font] [Default] [Show] [Resize] [History] [Profile]

Any ones

Return true if 1 occurs in the array, otherwise return false.


[Font] [Default] [Show] [Resize] [History] [Profile]

Words beginning with s


Return true if every element starts with the letter "s", otherwise return false.

f({"sock","shoe","spats"})    -> true
f({"silver","gold"})          -> false
f({"swim","swam","swan"})     -> true
f({"saturday","sunday"})      -> true


[Font] [Default] [Show] [Resize] [History] [Profile]