40923227 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • About
    • Develop
      • test
  • HomeWork
    • HW1
      • Overview
      • AGP Slots (AGP插槽)
      • Processors(處理器)
      • Video Displays and Video Cards(顯示器與顯示卡)
      • Video Output
      • Install and Configure Expansion Cards(安裝和配置擴充卡)
      • Storage Devices Overview(存儲設備概述)
      • Storage Devices Hard Disk Drives(儲存設備 硬碟驅動器)
      • Storage Devices  Hot Swapping(儲存設備 熱交換)
      • Heat Sources(熱源)
      • heat reduction
      • BIOS Settings(bios設置)
    • HW2
      • 作業2-1
      • 變數 Variables
      • HTML Tutorial HTML教程
    • HW3
      • Check Tic Tac Toe
      • Max Of Three
      • Tic Tac Toe Game
Check Tic Tac Toe << Previous Next >> Tic Tac Toe Game

Max Of Three

Implement a function that takes as input three variables, and returns the largest of the three. Do this without using the Python max() function!

The goal of this exercise is to think about some internals that Python normally takes care of for us. All you need is some variables and if statements!

Max Of Three Solutions

Sample solutions

There are many ways to answer this question, ranging from simple to complex. Here are a few reader-submitted answers!

有許多方法可以回答此問題,從簡單到復雜。這是讀者提交的一些答案!

This first example solution uses a series of if statements and comparisons to find the largest of 3 elements.

def max_of_three(a,b,c):
     max_3=0
     if a>b:
         #max_3=a
         if a>c:
             max_3=c
         else:
             max_3=a
     else:
          if b>c:
             max_3=b
          else:
             max_3=c
     return max_3

Another solution is a little bit less verbose, taking 3 numbers as an input, making them into a list, sorting them, and then reading off the largest element.

另一個解決方案是稍微冗長一些,將3個數字作為輸入,將它們放入列表,對它們進行排序,然後讀取最大的元素。

This last solution uses a more compact series of if statement comparisons to cover all cases of 3 elements.

#!/usr/bin/env python
import sys

if len(sys.argv) < 4:
    print 'Usage <value1> <value2> <value3>'
    sys.exit ( 1 )
    
arg1 = sys.argv[1]
arg2 = sys.argv[2]
arg3 = sys.argv[3]
def maxfunction(a,b,c):
	if (a > b) and (a > c):
	    print 'Max value is :',a
        elif (b > a) and (b > c):
            print 'Max value is :',b
        elif (c > a) and (c > b):
            print 'Max value is :',c
            
maxfunction(arg1,arg2,arg3)

Check Tic Tac Toe << Previous Next >> Tic Tac Toe Game

Copyright © All rights reserved | This template is made with by Colorlib