231. Power of Two python

231. Power of Two

Given an integer, write a function to determine if it is a power of two.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question


根据提示是用位运算,所以我就采用了python中的位运算

  1. class Solution(object):
  2.     def isPowerOfTwo(self, n):
  3.         """
  4.         :type n: int
  5.         :rtype: bool
  6.         """
  7.         if n<=0:
  8.             return False
  9.         a = bin(n).replace('0b','')
  10.         if a.count('1') == 1:
  11.             return True
  12.         return False

遇到的问题有两个:一个是对Python的自带函数不是很熟,需要查资料才做出来。

另外一个是,一开始的思路是打算用math.log函数计算,因为python的精度没有处理好。所以放弃。不得不说,python的精度还是麻烦

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Pre-whitening is a technique used in signal processing to remove the spectral correlation of a signal, thus making it easier to analyze or model. Here is an example of how to pre-whiten a signal using Python and the NumPy library. First, let's import the necessary libraries: ```python import numpy as np import matplotlib.pyplot as plt from scipy.signal import lfilter, butter ``` Next, let's generate a simple signal consisting of two sinusoids with different frequencies and amplitudes: ```python fs = 1000 # Sampling rate in Hz t = np.arange(0, 1, 1/fs) # Time vector from 0 to 1 second n = len(t) # Number of samples f1 = 50 # First sinusoid frequency in Hz f2 = 200 # Second sinusoid frequency in Hz A1 = 1 # First sinusoid amplitude A2 = 0.5 # Second sinusoid amplitude x = A1*np.sin(2*np.pi*f1*t) + A2*np.sin(2*np.pi*f2*t) # Signal ``` We can plot the signal to visualize it: ```python plt.plot(t, x) plt.xlabel('Time (s)') plt.ylabel('Amplitude') plt.show() ``` ![Signal plot](https://i.imgur.com/lNPF9fn.png) Now we can pre-whiten the signal using a first-order Butterworth high-pass filter with a cutoff frequency of 10 Hz. This will remove the low-frequency components of the signal and leave us with a white noise signal: ```python f_cutoff = 10 # Cutoff frequency in Hz b, a = butter(1, f_cutoff/(fs/2), btype='highpass') # High-pass filter coefficients x_filt = lfilter(b, a, x) # Apply filter to signal ``` We can plot the filtered signal to visualize it: ```python plt.plot(t, x_filt) plt.xlabel('Time (s)') plt.ylabel('Amplitude') plt.show() ``` ![Filtered signal plot](https://i.imgur.com/vhn6UFW.png) As you can see, the pre-whitened signal has a flat spectral density, which means that its power is uniformly distributed across all frequencies. This makes it easier to analyze or model the signal without being biased by its spectral correlation.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆浆456

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值