fork download
  1. def isContains5(x):
  2. """Returns True if the given integer contains the digit 5 exactly once."""
  3. s = str(x)
  4. count5 = 0
  5. for c in s:
  6. if c == '5':
  7. count5 += 1
  8. return count5 == 1
  9.  
  10. def main():
  11. """How many integers from 1 to 1000 contain the digit 5 exactly once?"""
  12. allNumbers = ""
  13. count = 0
  14. x = 1
  15. while x <= 1000:
  16. if isContains5(x):
  17. allNumbers += str(x)+","
  18. count += 1
  19. x += 1
  20. print(allNumbers[:-1])
  21. print("Count: "+str(count))
  22.  
  23. if __name__ == "__main__":
  24. main()
  25.  
Success #stdin #stdout 0.01s 7152KB
stdin
Standard input is empty
stdout
5,15,25,35,45,50,51,52,53,54,56,57,58,59,65,75,85,95,105,115,125,135,145,150,151,152,153,154,156,157,158,159,165,175,185,195,205,215,225,235,245,250,251,252,253,254,256,257,258,259,265,275,285,295,305,315,325,335,345,350,351,352,353,354,356,357,358,359,365,375,385,395,405,415,425,435,445,450,451,452,453,454,456,457,458,459,465,475,485,495,500,501,502,503,504,506,507,508,509,510,511,512,513,514,516,517,518,519,520,521,522,523,524,526,527,528,529,530,531,532,533,534,536,537,538,539,540,541,542,543,544,546,547,548,549,560,561,562,563,564,566,567,568,569,570,571,572,573,574,576,577,578,579,580,581,582,583,584,586,587,588,589,590,591,592,593,594,596,597,598,599,605,615,625,635,645,650,651,652,653,654,656,657,658,659,665,675,685,695,705,715,725,735,745,750,751,752,753,754,756,757,758,759,765,775,785,795,805,815,825,835,845,850,851,852,853,854,856,857,858,859,865,875,885,895,905,915,925,935,945,950,951,952,953,954,956,957,958,959,965,975,985,995
Count: 243