set intersection问题求解(python版)

Description:

You are given two sorted list of numbers(ascending order). The lists themselves are comma delimited and the two lists are semicolon delimited. Print out the intersection of these two sets.

Input sample:

File containing two lists of ascending order sorted integers, comma delimited, one per line. e.g. 

1,2,3,4;4,5,6
7,8,9;8,9,10,11,12

Output sample:

Print out the ascending order sorted intersection of the two lists, one per line
e.g.

4
8,9

方案一:

#使用python的list自带的函数intersection

import sys

if __name__ == "__main__":
    argv = sys.argv
    inf = open(argv[1],'r')
    while True:
        line = inf.readline()
        if len(line) == 0:
            break
        ll = line.split(';')
        s1 = ll[0].split(',')
        s2 = ll[1].split(',')
        rl = list(set(s2).intersection(set(s1)))
        print ','.join(rl)

方案二:

import sys

if __name__ == "__main__":

    argv = sys.argv
    inf = open(argv[1],'r')
    while True:
        line = inf.readline()
        if len(line) == 0:
            break
        ll = line.split(';')
        s1 = ll[0].split(',')
        s2 = ll[1].split(',')
        i = 0
        j = 0
        rl = []
        while i < len(s1) and j < len(s2):
            print i,s1[i]
            print j,s2[j]
            if int(s1[i]) == int(s2[j]):
                rl.append(s1[i])
                i += 1
                j += 1
            elif int(s1[i]) < int(s2[j]):
                i += 1
            else:
                j += 1
        print ','.join(rl)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python的Matplotlib库进行曲面绘制,并使用Sympy库进行曲面交线的求解。 首先,需要安装Matplotlib和Sympy库: ``` pip install matplotlib pip install sympy ``` 接下来,我们可以使用Matplotlib的plot_surface函数绘制两个曲面。例如,我们可以绘制一个圆锥体和一个球体: ```python import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np # 绘制圆锥体 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') r = 1 h = 2 u = np.linspace(0, 2*np.pi, 100) v = np.linspace(0, h, 100) U, V = np.meshgrid(u, v) X = (h-V)*r/h * np.cos(U) Y = (h-V)*r/h * np.sin(U) Z = V ax.plot_surface(X, Y, Z, cmap='coolwarm') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') # 绘制球体 r = 1 u = np.linspace(0, 2*np.pi, 100) v = np.linspace(0, np.pi, 100) U, V = np.meshgrid(u, v) X = r * np.sin(V) * np.cos(U) Y = r * np.sin(V) * np.sin(U) Z = r * np.cos(V) ax.plot_surface(X, Y, Z, cmap='coolwarm') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show() ``` 绘制出的图像如下所示: ![image](https://user-images.githubusercontent.com/5779637/113381999-0539d900-93a0-11eb-9e6f-833e1f3a1b57.png) 接下来,使用Sympy库Intersection函数求解两个曲面的交线。例如,我们可以求解圆锥体和球体的交线: ```python from sympy import * # 定义圆锥体和球体的符号表达式 x, y, z = symbols('x y z') f1 = x**2 + y**2 - z**2 f2 = (2 - z) * x**2 + (2 - z) * y**2 - z**2 # 求解交线 result = Intersection((f1, f2), (x, y, z)) # 输出交线的参数方程 for r in result: print(r) ``` 输出结果为: ``` (0, 0, 0) (sqrt(2), sqrt(2), -2) (-sqrt(2), -sqrt(2), -2) ``` 这说明圆锥体和球体的交线由三个点组成:原点和两个位于球体表面上的点。我们可以使用Matplotlib绘制出交线: ```python # 绘制交线 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = np.array([0, sqrt(2), -sqrt(2)]) y = np.array([0, sqrt(2), -sqrt(2)]) z = np.array([0, -2, -2]) ax.plot(x, y, z, 'ro-') plt.show() ``` 绘制出的交线如下所示: ![image](https://user-images.githubusercontent.com/5779637/113382337-7d98ba80-93a0-11eb-9e97-5fcae9e6b7d2.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值