Code Signal_练习题_almostIncreasingSequence

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Example

    • For sequence = [1, 3, 2, 1], the output should be
      almostIncreasingSequence(sequence) = false.

      There is no one element in this array that can be removed in order to get a strictly increasing sequence.

    • For sequence = [1, 3, 2], the output should be
      almostIncreasingSequence(sequence) = true.

      You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3]

 

 

我的解答: 

1 想半天想不出来,通过查阅各个大佬的做法,发现用这种方法的挺多
2 def almostIncreasingSequence(sequence):
3     count = 0
4     for i in range(len(sequence)-1):
5         if i+1 < len(sequence) and sequence[i] >= sequence[i+1]:
6             count += 1
7         if i+2 < len(sequence) and sequence[i] >= sequence[i+2]:
8             count += 1
9     return count < 3

 

膜拜大佬:

上个代码还能看懂,这个来自捷克共和国的一位大佬的代码真懵了..啥题都能一行代码解决...
def almostIncreasingSequence(s):
    return 3> sum((i >= j) + (i >= k) for i, j, k in zip(s, s[1:], s[2:] + [10**6]))
View Code

 

转载于:https://www.cnblogs.com/BlameKidd/p/9348528.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,这是一个生成信号的函数,它可以生成一个包含载波和码元的复合信号。以下是一个可能的实现: ```matlab function signal_r = signal_gen(fsample, f_carrier, f_code, code_phase_init, dBW_signal_pow, dB_C_I, dot_num) % Inputs: % - fsample: 采样频率 % - f_carrier: 载波频率 % - f_code: 码元频率 % - code_phase_init: 码元初始相位 % - dBW_signal_pow: 信号功率 % - dB_C_I: 载波与干扰的功率比 % - dot_num: 码元个数 % Output: % - signal_r: 生成的信号 % 计算采样间隔 T = 1/fsample; % 计算码元时长 T_code = 1/f_code; % 生成时间轴 t = (0:T:(dot_num-1)*T_code)'; % 生成码元 code = sign(sin(2*pi*f_code*t + code_phase_init)); % 生成带有干扰的载波 carrier = sqrt(10^(dBW_signal_pow/10)) * cos(2*pi*f_carrier*t) ... + sqrt(10^((dBW_signal_pow-dB_C_I)/10)) * randn(size(t)); % 生成复合信号 signal_r = carrier .* code; ``` 这个函数中,我们首先计算了采样间隔 $T$,然后根据码元频率 $f_\text{code}$ 计算了码元时长 $T_\text{code}$。然后,我们生成了时间轴 $t$,包含了所有的采样点。接着,我们生成了码元信号 $code$,使用了 sign 函数将正弦波转换为方波。然后,我们生成了带有干扰的载波 $carrier$,其中信号功率 $P_\text{signal}$ 的 dB 值为 $dBW_\text{signal\_pow}$,干扰功率 $P_\text{interference}$ 的 dB 值为 $dB_\text{C_I}$,并且使用 randn 函数生成了一个高斯白噪声序列,其功率为 $P_\text{interference}$。最后,我们生成了复合信号 $signal_r$,其为载波和码元信号的乘积。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值