파이썬 질문입니다!!

익명 사용자의 이미지

제가 아래처럼 코드를 짜보았는데 궁금한게 생겨서 질문드립니다.

SIZE = 4
 
for i in range(0, (1<<SIZE), 1):
    sum = 0
    for j in range(SIZE-1, -1, -1):
        if (i >> j) & 1 == 0:
            sum = sum + 1
    if sum == 2:
        for j in range(SIZE-1, -1, -1):
            print(( i >> j ) & 1, end = ' ')
 
                if j % 2 == 0:
                    print(' ')
        print(' ')
 

코드 내용을 간단하게 설명드리면 10진수 0~15를 2진수 0000~1111 표현하는 코드입니다.

if sum == 2: 은 16개 중 0의 개수가 2개인 것만 따로 추출하는 것입니다. 그러면 6개가 나오게 됩니다.

그리고 추출한 수를 2x2 행렬 모양처럼 만드는 과정입니다.

제가 궁금한거는 2x2를 4x4로 변형시키는 방법을 알고싶습니다...

무슨 말이냐면 예시로 0 1 1 0 으로 가지고 해보겠습니다.

코드를 실행시키면 아래 왼쪽과 같이 나오게 되는데 이를 오른쪽처럼 확장? 하는 것입니다.

0 1 ------> 0 1 0 1
1 0         1 0 1 0
            0 1 0 1
            1 0 1 0

p.s. SIZE 를 해준 이유는 크기를 자유롭게 조절 할 수 있게 하려고 설정한 것입니다..
raymundo의 이미지

음... "2x2를 4x4로 변형"을 어떻게 하겠다는 말인지가 명확하지 않습니다.
예시를 들어주셨으나 여전히 해석하기 나름이긴 한데, 눈에 먼저 들어오는 대로라면 다음과 같이 하시려는 것 같네요.

a b
c d
배열을 가로 세로로 두 번씩 써서
a b a b
c d c d
a b a b
c d c d

SIZE = 4
# SIZE가 6이라면 2x3 또는 3x2 일 수 있으니...
WIDTH, HEIGHT = 2, 2
 
 
for i in range(0, (1<<SIZE), 1):
    sum = 0
 
    for j in range(SIZE-1, -1, -1):
        if (i >> j) & 1 == 0:
            sum = sum + 1
 
    if sum == 2:
        for row in range(HEIGHT*2-1, -1, -1):
            for col in range(WIDTH*2-1, -1, -1):
                position = WIDTH*(row % HEIGHT) + (col % WIDTH)
                print (i >> position) & 1,
            print(' ')
 
        print(' ')

좋은 하루 되세요!

질문자의 이미지

우선, 위에 a b c d 써주신 것처럼 하는게 맞습니다!
근데 왜 코드에 오류가 발생하는지 아시나요..?

작성자의 이미지

아 해결햇습니다!
print ((i >> position) & 1, end = '')
이렇게 수정해줘야 제대로 나오네요 ㅎㅎ
감사합니다 ㅜㅜㅜ

raymundo의 이미지

아, 제가 파이썬2로 하느라고 고쳤던 부분인가 봅니다. ^^;

좋은 하루 되세요!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.