0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonでコーディング問題を解く1

Last updated at Posted at 2023-03-13

pythonを学びたいpython初心者がコーディング問題を1-3月にかけて解いたのでメモを残します。
全部で6回あります。

問題1

10進表記された数を反転する関数を作ってください

import math

def reverseNumber(value) :
    result = 0

    while value > 0 :
        # 入力値の1桁目を取り出して、それまでの合計を10倍した値に足すのを繰り返す
        reminder = value % 10
        result = result * 10 + reminder
        
        value = math.floor(value / 10)
        
    return result

print(reverseNumber(12345))
print(reverseNumber(10345))
print(reverseNumber(10340))

print(reverseNumber(100))
print(reverseNumber(10200))

問題2 3進数を7進数に変換してください

import math

def convert3to10(value) :
    result = 0
    exp = 1

    while value > 0 :
        reminder = value % 10
        result += reminder * exp

        exp *= 3
        value = math.floor(value / 10)

    return result
    
def convert10to7(value) :
    result = ""
    
    exp = 1
    
    while value > 0 :
        reminder = value % 7
        result = str(reminder) + result
        value = math.floor(value / 7)
        
    return result 
    

print(convert3to10(111), convert10to7(convert3to10(111)))
print(convert3to10(1111), convert10to7(convert3to10(1111)))
print(convert3to10(1000100100), convert10to7(convert3to10(1000100100)))

問題3 10進数を16進数に変換してください

import math

list = ['A','B','C','D','E','F']
def getHexNumber (value) :
    if value >= 10 :
        return list[value - 10]
    else :
        return str(value)

def convert10to16(value) :
    result = ""
    
    exp = 1
    
    while value > 0 :
        result = getHexNumber(value % 16) + result
        value = math.floor(value / 16)
        
    return result 

print(convert10to16(327), hex(327)) #hexは比較用
print(convert10to16(319), hex(319)) #hexは比較用
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?