我需要检查一些二进制数字是否可以被2或8整除,并告诉它们有多少个二进制数。现在,我的二进制数在最后一位为0时可被2整除,而在最后3为0时可被8整除,所以这是我的操作方式
twos = 0
eights = 0
file = 'numbers.txt'
with open(file) as fin:
for line in fin:
if line[-2:] == '0':
twos += 1
elif line[-3:] == '000':
eights +=1
print(twos)
print(eights)
tbh我现在不知道为什么它不起作用,我敢打赌这是因为数据类型不同,但是我对python还是陌生的,无法弄清楚错误在哪里
numbers.txt样本
答案 0 :(得分:2)
最后一个“数字”是class ViewItemAdapter(
val activity: Activity,
val itemList: MutableList<LinkedHashMap<String, Any>>,
val mDragStartListener: OnStartDragListener,
val recyclerView: RecyclerView,
val nestedScrollView: NestedScrollView
) : RecyclerView.Adapter<RecyclerView.ViewHolder>(), ItemTouchHelperAdapter {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return ItemDisplayRankableHolder(layoutInflater.inflate(R.layout.item_display_rankable, parent, false))
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val hashMap = itemList[position]
val nameString = hashMap["name"].toString()
if (holder is ItemDisplayRankableHolder) {
holder.tvItemName.text = nameString
if (hashMap.containsKey("description")) {
val descString = hashMap["description"].toString()
holder.tvItemDescription.text = descString
} else {
holder.tvItemDescription.visibility = View.GONE
}
}
holder.itemView.setOnClickListener {
log("RecyclerView: ${recyclerView.measuredHeight}")
log("Height: ${holder.itemView.measuredHeight}")
}
}
override fun getItemCount(): Int {
return itemList.size
}
override fun onItemMove(fromPosition: Int, toPosition: Int) {
if (toPosition < itemList.size) {
if (fromPosition < toPosition) {
for (i in fromPosition until toPosition) {
Collections.swap(itemList, i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(itemList, i, i - 1)
}
}
notifyItemMoved(fromPosition, toPosition)
}
}
}
(或class ListItemFragment : Fragment(), OnStartDragListener, ArrayResponse {
private lateinit var nestedScrollView: NestedScrollView
private lateinit var indexRecyclerView: RecyclerView
private lateinit var itemRecyclerView: RecyclerView
private var mItemTouchHelper: ItemTouchHelper? = null
private lateinit var itemList: MutableList<LinkedHashMap<String, Any>>
var currentScrollY = 0
var lastScrollY = 0
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment_list_item, container, false)
initAll(rootView)
val bundle = arguments
if (bundle != null) {
val itemData = bundle.getString("list")
val collectionType = object : TypeToken<MutableList<LinkedHashMap<String, Any>>>() {}.type
itemList = Gson().fromJson(itemData, collectionType)
startAdapter()
}
nestedScrollView.setOnScrollChangeListener { v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int ->
val number = scrollY / DISPLAY_ITEM_HEIGHT
currentScrollY = scrollY
(parentFragment as ListDetailsFragment).showTopRemaining(number)
(parentFragment as ListDetailsFragment).showBottomRemaining(number)
}
return rootView
}
private fun initAll(rootView: View) {
indexRecyclerView = rootView.findViewById(R.id.indexRecyclerView)
indexRecyclerView.setHasFixedSize(true)
indexRecyclerView.layoutManager = LinearLayoutManager(context)
itemRecyclerView = rootView.findViewById(R.id.itemRecyclerView)
itemRecyclerView.setHasFixedSize(true)
itemRecyclerView.isNestedScrollingEnabled = false
itemRecyclerView.layoutManager = LinearLayoutManager(context)
nestedScrollView = rootView.findViewById(R.id.nestedScrollView)
itemList = mutableListOf()
}
private fun startAdapter() {
val indexAdapter = IndexAdapter(activity!!, itemList.size)
indexRecyclerView.adapter = indexAdapter
val itemAdapter = ViewItemAdapter(activity!!, itemList, this, itemRecyclerView, nestedScrollView)
itemRecyclerView.adapter = itemAdapter
val callback = DisplayItemTouchHelperCallback(itemAdapter, itemRecyclerView, nestedScrollView, this)
mItemTouchHelper = ItemTouchHelper(callback)
mItemTouchHelper?.attachToRecyclerView(itemRecyclerView)
}
override fun onStartDrag(viewHolder: RecyclerView.ViewHolder) {
mItemTouchHelper?.startDrag(viewHolder)
}
override fun onValuePass(responseList: LinkedHashMap<String, Any>) {
if (responseList.containsKey("scroll")) {
val height = responseList["scroll"] as Int
nestedScrollView.smoothScrollTo(0, height)
}
}
}
),而不是line[-1]
(根据line[-1:]
是最后3个数字的相同逻辑”个数字”,而不是最后2个数字。
您的代码根本不会尝试处理换行符。
您的算法将丢失由于line[-2:]
而被2和8整除的数字。
line[-3:]
应成为:
elif
答案 1 :(得分:-1)
正如您所说,当最后一位为0时,二进制数可被2整除:
line = line.strip() // The strip() method returns a copy of the string with both leading and trailing characters removed
if line[-1] == '0':
twos += 1