python作业第三题Cable car

Cable car

COMP9021, Session 2, 2018

描述

  • 下图是为电车提供运行支撑的电杆和电缆,相邻电杆间等距,电杆顶端有高度标识。如果连续电缆的斜率不变,则电杆构成了一次良好的运行。如图,最长的良好运行介于电杆高度25到28之间,电缆被标识为蓝色,长度为3(任意单位)。


    图一
  • If one gets rid of the black pillars and keeps only the remaining (red) ones, then one can use use the latter and create a new structure (with successive pillars being equidistant) which makes up for a perfect ride, that is, good from first to last pillar:


    图二
  • It is the longest perfect ride one can obtain this way; so 5, the number of black pillars, is the minimal number of pillars to remove to build a perfect ride from the rest.
  • Write a program, stored in a file named cable_car.py, that performs the following task.
    • The program prompts the user to input a file name. If there is no file with that name in the working directory, then the program outputs an error message and exits.
    • The contents of the file should consist of a strictly increasing sequence of at least two strictly positive integers. Consecutive numbers are separated by at least one space, and there can be extra spaces, including empty lines, anywhere. If that is not the case then the program outputs another error message and exits.
    • These numbers are meant to represent the heights of pillars to build a cable car structure. The program then outputs:
      • whether or not this structure makes up for a perfect ride;
      • the length of the longest good ride;
      • how many pillars to remove to build a perfect ride from the remaining ones.

import os

#argv = sys.argv
#file_path = argv[1]

def isperfect(nums):

    s = nums[1]-nums[0]
    for i,value in enumerate(nums):
        if i == 0:
            continue
        if value - nums[i-1] != s:
            print('The ride could be better...')
            return
    print('The ride is perfect!')

def longest_good_ride(nums):

    d_value = list()

    for i,value in enumerate(nums):
        if i == 0:
            continue
        d_value.append(value - nums[i-1])

    good_rides = list()
    current = 1
    for i,value in enumerate(d_value):
        if i == 0:
            current = 1
            continue
        if value == d_value[i-1]:
            current=current+1
            if i == len(d_value)-1:
                good_rides.append(current)
        else:
            good_rides.append(current)
            current=1
            if i == len(d_value)-1:
                good_rides.append(current)
    print('The longest good ride has a length of:',max(good_rides))


def min_pillars(nums):
    goodpillars_list = list()
    for i in range(len(nums)):
        # 以每个柱子为基住,获取基柱最大的斜率
        slopes = list()
        for pos in range(len(nums)):
            if pos==i:
                continue
            slopes.append(abs(nums[pos] - nums[i]))
        min_slope = min(slopes)
        max_slope = max(slopes)
        # 从1到max_slope便利每种斜率下,与该基柱斜率相同的柱子数量
        
        for slop_ in range(min_slope,max_slope+1):
            goodpillars = 0
            for pos_ in range(i,len(nums)):
                if abs(goodpillars*slop_) == abs(nums[i] - nums[pos_]):
                    goodpillars = goodpillars+1
            goodpillars_list.append(goodpillars)
            for pos_ in range(0,i):
                if abs(goodpillars*slop_) == abs(nums[i] - nums[pos_]):
                    goodpillars = goodpillars+1
            goodpillars_list.append(goodpillars)

    max_goodpillar = max(goodpillars_list)

    print('The minimal number of pillars to remove to build a perfect ride from the rest is:',len(nums)-max_goodpillar)


if __name__ == '__main__':
    str = input('Please enter the name of the file you want to get data from:');
    
    nums = list()
    try:
        with open(str,'r') as f:
            for l in f:
                strnums = l.split()
                for strnum in strnums:
                    try:
                        nums.append(int(strnum))
                    except ValueError:
                        print('Sorry, input file does not store valid data.')
                        exit()
    except IOError:
        print('Sorry, there is no such file.')
        exit()
    if len(nums) <= 1:
        print('Sorry, input file does not store valid data.')
        exit()

    for i ,value in enumerate(nums):
        if i == 0:
            if value <= 0:
                print('Sorry, input file does not store valid data.')
                exit()
            continue
        if nums[i-1] > value:
            print('Sorry, input file does not store valid data.')
            exit()

    isperfect(nums)
    longest_good_ride(nums)
    min_pillars(nums)

    pass


你可能感兴趣的:(python作业第三题Cable car)