Jump to content

Trinary Code

- - - - -

  • Please log in to reply
12 replies to this topic

#1
harlequin-wesley

harlequin-wesley

    Newbie

  • Members
  • Pip
  • 1 posts
Hi, I am a newbie in Python. Recently I'm working with a project called "Trinary Code".
This program is to convert string data into trinary digit, as follow:


Trinary Digit


9 3 1


0 0 0 = 0	<space>


0 0 1 = 1	a

0 0 2 = 2	b

0 1 0 = 3	c

0 1 1 = 4	d

0 1 2 = 5	e


0 2 0 = 6	f

0 2 1 = 7	g

0 2 2 = 8	h

1 0 0 = 9	i

1 0 1 = 10	j


1 0 2 = 11	k

1 1 0 = 12	l

1 1 1 = 13	m

1 1 2 = 14	n

1 2 0 = 15	o


1 2 1 = 16	p

1 2 2 = 17	q

2 0 0 = 18	r

2 0 1 = 19	s

2 0 2 = 20	t


2 1 0 = 21	u

2 1 1 = 22	v

2 1 2 = 23	w

2 2 0 = 24	x

2 2 1 = 25	y

2 2 2 = 26	z



The codes are as follow:

#! /usr/local/bin/python3.2


### This program converts data into trinary codes ###

 

def algorithm(x):


	if x == ' ':

		print('000')


	elif x == 'a' or x =='A':

		print('001')


	elif x == 'b' or x =='B':

		print('002')


	elif x == 'c' or x == 'C':

		print('010')


	elif x == 'd' or x == 'D':

		print('011')


	elif x == 'e' or x == 'E':

		print('012')


	elif x == 'f' or x == 'F':

		print('020')


	elif x == 'g' or x == 'G':

		print('021')


	elif x == 'h' or x == 'H':

		print('022')


	elif x == 'i' or x == 'I':

		print('100')


	elif x == 'j' or x == 'J':

		print('101')


	elif x == 'k' or x == 'K':

		print('102')


	elif x == 'l' or x == 'L':

		print('110')


	elif x == 'm' or x == 'M':

		print('111')


	elif x == 'n' or x == 'N':

		print('112')


	elif x == 'o' or x == 'O':

		print('120')


	elif x == 'p' or x == 'P':

		print('121')


	elif x == 'q' or x == 'Q':

		print('122')


	elif x == 'r' or x == 'R':

		print('200')

	

	elif x == 's' or x == 'S':

		print('201')


	elif x == 't' or x == 'T':

		print('202')


	elif x == 'u' or x == 'U':

		print('210')


	elif x == 'w' or x == 'W':

		print('212')


	elif x == 'x' or x == 'X':

		print('220')


	elif x == 'y' or x == 'Y':

		print('221')


	elif x == 'z' and x == 'Z':

		print('222')


string = input('\nEnter a string: ')


for eachLetter in string:

	algorithm(eachLetter)



I'm seeking ways to simplify the function "algorithm(x)", and also to improve this program into an encryption system~ ^_^

#2
nikpalumbo

nikpalumbo

    Newbie

  • Members
  • Pip
  • 5 posts
Hi,
try this code:

def algorithm(x):

   return '000' if x ==' ' else bin(ord(x.upper()) - 64)[2:].zfill(3)

You can also to improve previouse code, because when you pass a value not in range 'A' - 'z' you can have a bit problem! :)

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
This could be done with a lookup table to convert it to ternary base (in your definition), I do not know Python syntax although it would look like this:

string = "Foobar"
array dectoter = ['a':'000', 'b':'001', ...etc... ]
for each character in string:
    print dectoter[character]

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
nikpalumbo

nikpalumbo

    Newbie

  • Members
  • Pip
  • 5 posts
I think my solution is simple or not?

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

nikpalumbo said:

I think my solution is simple or not?
It would personally be hard to maintain or extend, and could be slow on large data sets. Mine does not use any function calls and scales quite easily to different bases.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
nikpalumbo

nikpalumbo

    Newbie

  • Members
  • Pip
  • 5 posts

Alexander said:

It would personally be hard to maintain or extend, and could be slow on large data sets. Mine does not use any function calls and scales quite easily to different bases.

if you need all characters, do will you write a lookup table by hand??

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
His code may handle specific characters in different ways. For example symbols are below A on the ASCII table, and they would become negative with your solution.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
nikpalumbo

nikpalumbo

    Newbie

  • Members
  • Pip
  • 5 posts

Alexander said:

His code may handle specific characters in different ways. For example symbols are below A on the ASCII table, and they would become negative with your solution.

Him code seems may handle ONLY a range characters and the range has a logic.
Infact if he gives in input characters below A on Ascii table he gets a None result!!

I said, my code might be improved like your solution that gets a runtime error if the you give a characters under A on Ascii table.

Regarding to "hard to maintain or extend", personally, I think if we haven't details of future applications..We can't make assumptions.

I'm too lazy to write a lookup table for all characters :)

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
The main reason I had suggested is his/her quote " and also to improve this program into an encryption system~"

Likely more than that range would be needed for an encryption that supports periods and other characters. That is why he handles space separately, he likely did not think of other characters that would be normally used and would add more of those justifying a table to look up.

Arrays can be serialized as well, and also a cipher could be applied (i.e. character+1) if that were ever his choice.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
nikpalumbo

nikpalumbo

    Newbie

  • Members
  • Pip
  • 5 posts

Alexander said:

The main reason I had suggested is his/her quote " and also to improve this program into an encryption system~"

Likely more than that range would be needed for an encryption that supports periods and other characters. That is why he handles space separately, he likely did not think of other characters that would be normally used and would add more of those justifying a table to look up.

Arrays can be serialized as well, and also a cipher could be applied (i.e. character+1) if that were ever his choice.

Seems a personal question!!! ;)

If He really wants create a encryption system, I think he just needs a way to convert character into a number and then he has to have to apply i.e a functions injective.

#11
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Very true, I know we answered a little late (by a few weeks), it would be neat if he could post more about his project!
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#12
THX

THX

    Newbie

  • Members
  • Pip
  • 5 posts

harlequin-wesley said:

Hi, I am a newbie in Python. Recently I'm working with a project called "Trinary Code".
This program is to convert string data into trinary digit, as follow:


Trinary Digit


9 3 1


0 0 0 = 0	<space>


0 0 1 = 1	a

0 0 2 = 2	b

0 1 0 = 3	c

0 1 1 = 4	d

0 1 2 = 5	e


0 2 0 = 6	f

0 2 1 = 7	g

0 2 2 = 8	h

1 0 0 = 9	i

1 0 1 = 10	j


1 0 2 = 11	k

1 1 0 = 12	l

1 1 1 = 13	m

1 1 2 = 14	n

1 2 0 = 15	o


1 2 1 = 16	p

1 2 2 = 17	q

2 0 0 = 18	r

2 0 1 = 19	s

2 0 2 = 20	t


2 1 0 = 21	u

2 1 1 = 22	v

2 1 2 = 23	w

2 2 0 = 24	x

2 2 1 = 25	y

2 2 2 = 26	z



The codes are as follow:

#! /usr/local/bin/python3.2


### This program converts data into trinary codes ###

 

def algorithm(x):


	if x == ' ':

		print('000')


	elif x == 'a' or x =='A':

		print('001')


	elif x == 'b' or x =='B':

		print('002')


	elif x == 'c' or x == 'C':

		print('010')


	elif x == 'd' or x == 'D':

		print('011')


	elif x == 'e' or x == 'E':

		print('012')


	elif x == 'f' or x == 'F':

		print('020')


	elif x == 'g' or x == 'G':

		print('021')


	elif x == 'h' or x == 'H':

		print('022')


	elif x == 'i' or x == 'I':

		print('100')


	elif x == 'j' or x == 'J':

		print('101')


	elif x == 'k' or x == 'K':

		print('102')


	elif x == 'l' or x == 'L':

		print('110')


	elif x == 'm' or x == 'M':

		print('111')


	elif x == 'n' or x == 'N':

		print('112')


	elif x == 'o' or x == 'O':

		print('120')


	elif x == 'p' or x == 'P':

		print('121')


	elif x == 'q' or x == 'Q':

		print('122')


	elif x == 'r' or x == 'R':

		print('200')

	

	elif x == 's' or x == 'S':

		print('201')


	elif x == 't' or x == 'T':

		print('202')


	elif x == 'u' or x == 'U':

		print('210')


	elif x == 'w' or x == 'W':

		print('212')


	elif x == 'x' or x == 'X':

		print('220')


	elif x == 'y' or x == 'Y':

		print('221')


	elif x == 'z' and x == 'Z':

		print('222')


string = input('\nEnter a string: ')


for eachLetter in string:

	algorithm(eachLetter)



I'm seeking ways to simplify the function "algorithm(x)", and also to improve this program into an encryption system~ ^_^

Trinary code is only apply to Python?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users