Records

SQL> drop table myoracle;

   

表已删除。

   

SQL> create table zuser (id number(4) primary key, fn char(10), sn char(10));

   

表已创建。

   

SQL> create sequence seq_zid

2 minvalue 1

3 nomaxvalue

4 start with 1

5 increment by 1

6 nocycle

7 nocache;

   

序列已创建。

   

SQL> create or replace trigger tg_zid

2 before insert on zuser for each row when (new.id is null)

3 begin select seq_zid.nextval into:new.id from dual;

4 end;

5 /

   

触发器已创建

   

SQL> show err;

   

SQL> select seq_zid.nextval from dual;

   

NEXTVAL

----------

1

   

SQL> select seq_zid.currval from dual;

   

CURRVAL

----------

1

   

SQL> desc dual

名称 是否为空? 类型

----------------------------------------- -------- ----------------------------

   

DUMMY VARCHAR2(1)

   

SQL> select * from all_sequences where lower(SEQUENCE_NAME) like '%seq_zid%';

   

SQL> select * from dba_objects where lower(object_name) = 'dual';

   

开始使用SQL DEVELOPER

   

ALTER SEQUENCE seq_zid increment by -1;

ALTER SEQUENCE seq_zid MINVALUE 0;

SELECT seq_zid.nextval FROM dual;

ALTER SEQUENCE seq_zid increment by 1;

ALTER SEQUENCE seq_zid MINVALUE 1;

   

INSERT INTO zuser VALUES (null,'Zheng','Yan');

   

ID FN SN

---------------------- ---------- ----------

1 Zheng Yan

   

alter table zuser drop constraint SYS_C009773;

   

alter table zuser add constraint pk_id primary key (id);

   

select * from dba_constraints where lower(table_name) = 'zuser';

   

alter table zuser add (salary number);

update zuser set salary = 1000+ mod(id,3)*100;

select avg(salary) avg, sum(salary) sum, max(salary) max, min(salary) min from zuser;

select fn,salary, count(*) "# Cost" from zuser group by fn, salary;

   

define a = 'Zheng';

select * from zuser where upper(fn) = upper('&a'); --一般字符和日期类型要加单引号

   

<think>我们已知records是一个列表,每个元素是一个包含姓名和分数的元组,例如:[('Alice', 90), ('Bob', 80), ...] 按照题目要求: 1. 计算所有参赛者分数的中位数。中位数的定义:排序后,如果数据个数为奇数,则取中间那个数;如果为偶数,则取中间两个数中较小的那个。 2. 然后,返回分数等于中位数的参赛者姓名列表,如果有多个,则按输入顺序的逆序输出。 步骤: 第一步:提取分数并排序,然后确定中位数。 第二步:遍历原始记录(按输入顺序的逆序),找出分数等于中位数的所有记录,并将姓名加入列表。 注意点: - 逆序输出:由于题目要求按输入顺序的逆序,我们可以逆序遍历原始列表,然后顺序收集符合条件的姓名。这样,在结果列表中,原始输入中靠后的记录会先出现。 - 中位数的计算:注意偶数情况取中间两个中较小的。 但这里有个细节:题目要求逆序输出,即如果输入顺序是A,B,C,且都是中位数分数,则输出应为C,B,A。但是,如果我们在第一次遍历原始记录时从后往前遍历,那么就会先遇到C,然后B,然后A,我们按顺序添加,那么结果列表就是[C, B, A]。 所以我们可以先按顺序提取分数列表(不改变原始顺序)进行排序计算中位数,然后逆序遍历原始记录。 代码: 定义函数:def median_ranks(records: List[Tuple[str, int]]) -> List[str]: scores = [score for _, score in records] # 提取分数 sorted_scores = sorted(scores) # 排序 n = len(sorted_scores) if n % 2 == 1: median = sorted_scores[n//2] else: median = sorted_scores[n//2 - 1] # 因为偶数情况取中间两个中较小的,即索引为n//2-1 然后,我们逆序遍历records列表(即从最后一个元素到第一个元素): result = [] for i in range(len(records)-1, -1, -1): name, score = records[i] if score == median: result.append(name) 或者,我们可以使用反向迭代器: result = [] for record in reversed(records): name, score = record if score == median: result.append(name) 返回result 注意:这样得到的结果就是按输入顺序的逆序输出的,因为先遍历到的是原列表中后面的元素。 但是,如果我们在计算中位数和遍历的时候,原列表records的顺序并没有改变,所以这个方法是可行的。 另外,如果列表为空,我们也需要处理。但是题目没有特别说明,我们可以按照一般情况处理。当列表为空时,n=0,那么中位数的计算就会进入到偶数分支,取sorted_scores[0//2-1]即-1索引,这会导致错误。所以我们需要单独处理空列表。 修正: 如果记录为空,直接返回空列表。 更新后的代码: 下面我们写出完整代码,并注意细节: </think> ### 竞赛结果中位数姓名提取实现(解决给定输入类型) ```python from typing import List, Tuple def find_median_competitors(records: List[Tuple[str, int]]) -> List[str]: # 处理空输入情况 if not records: return [] # 提取所有分数 scores = [score for _, score in records] n = len(scores) sorted_scores = sorted(scores) # 计算中位数 if n % 2 == 1: median = sorted_scores[n//2] else: median = sorted_scores[n//2 - 1] # 按输入逆序收集姓名 result = [] # 使用逆序索引遍历保证原始顺序逆序 for i in range(len(records)-1, -1, -1): name, score = records[i] if score == median: result.append(name) return result ``` ### 关键实现细节说明 1. **输入类型处理**: - `records: List[Tuple[str, int]]` 明确指定输入类型 - 使用`for _, score in records`安全解包元组 - 空列表处理:`if not records: return []` 2. **中位数计算优化**: - 直接排序分数列表`sorted(scores)`而无需复制元组 - 利用整数除法特性计算中位数索引 - 奇数:`n//2`(如n=5→索引2) - 偶数:`n//2 -1`(如n=4→索引1) 3. **逆序输出实现**: ```mermaid graph TD 原始顺序 --> 索引0_Alice --> 索引1_Bob --> 索引2_Charlie 逆序遍历 --> 索引2_Charlie --检查--> 添加 索引2_Charlie --> 索引1_Bob --检查--> 添加 索引1_Bob --> 索引0_Alice --检查--> 添加 ``` - 通过`range(len(records)-1, -1, -1)`实现反向索引遍历 - 保持原始输入顺序逆序(后出现者优先) 4. **时间复杂度O(n log n)**: - 排序操作:O(n log n) 主导时间复杂度 - 两次遍历:O(n) ### 示例验证 ```python # 测试样例1: 奇数个参赛者 print(find_median_competitors( [('Alice', 90), ('Bob', 80), ('Charlie', 70)] )) # 输出: ['Bob'] (中位数80) # 测试样例2: 偶数个参赛者 print(find_median_competitors( [('D', 85), ('E', 95), ('F', 85), ('G', 90)] )) # 排序[85,85,90,95]→中位数85 # 逆序遍历: G→F→E→D → 输出['F','D'](F位置在D之后) # 测试样例3: 所有分数相同 print(find_median_competitors( [('H', 100), ('I', 100), ('J', 100)] )) # 输出: ['J','I','H'] 逆序输出 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值