91
58
54
108
52
18
8
81
103
110
129
137
84
15
14
18
11
17
12
6
1
28
6
14
8
8
0
0
28
24
25
23
21
13
9
4
18
17
18
30
13
3
I want to split into chunks of six entries each.After that it will break the loop.Then it will continue the entries 7..12, then of 13..18 etc.
(for loop?continue?break?)
You can use paste:
paste -d' ' - - - - - - < inputfile
For your input, it'd return:
91 58 54 108 52 18
8 81 103 110 129 137
84 15 14 18 11 17
12 6 1 28 6 14
8 8 0 0 28 24
25 23 21 13 9 4
18 17 18 30 13 3
$ xargs -n 6 < file_name
91 58 54 108 52 18
8 81 103 110 129 137
84 15 14 18 11 17
12 6 1 28 6 14
8 8 0 0 28 24
25 23 21 13 9 4
18 17 18 30 13 3
Related
I am trying to produce a square-formatted multiplication table with the output at the end using code below:
def multiplicationTable(maxValue):
for i in range(1, maxvalue):
for j in range(1, maxvalue):
print(("{:6d}".format(i * j,)), end='')
print()
print(multiplicationTable(1)
print(multiplicationTable(5))
print(multiplicationTable(10))
1
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
I get an error:
File "", line 7
print(multiplicationTable(5))
^
SyntaxError: invalid syntax
print(multiplicationTable(1) is missing a closing ).
You are using maxValue (with capital V) in your function definition while using maxvalue (with small v) in the function body.
Here is the new version:
def multiplicationTable(maxvalue): # maxvalue, not maxValue
for i in range(1, maxvalue+1):
for j in range(1, maxvalue+1):
print(("{:6d}".format(i * j,)), end='')
print()
multiplicationTable(1)
multiplicationTable(5)
multiplicationTable(10)
EDIT 1: Changed range(1, maxvalue) to range(1, maxvalue+1)
EDIT 2: Changed print(multiplicationTable(n)) to multiplicationTable(n)
Here is a sample input text file generated with the cal command:
$ cal 2743 > sample_text
In this example this file have 2180 characters
$ wc sample_text
36 462 2180 sample_text
I want to split it into smaller files each one having no more than 700 lines but preserving lines in complete state (no line can be cut)
I can view each such block with following awk code:
$ awk '{l=length+l;if(l<=700){print l,$0}else{l=length;print "\nnext block\n",l,$0}}' sample_text
32 2743
98 January February March
164 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
230 1 2 1 2 3 4 5 6 1 2 3 4 5 6
296 3 4 5 6 7 8 9 7 8 9 10 11 12 13 7 8 9 10 11 12 13
362 10 11 12 13 14 15 16 14 15 16 17 18 19 20 14 15 16 17 18 19 20
428 17 18 19 20 21 22 23 21 22 23 24 25 26 27 21 22 23 24 25 26 27
494 24 25 26 27 28 29 30 28 28 29 30 31
560 31
560
626 April May June
692 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
next block
66 1 2 3 1 1 2 3 4 5
132 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12
198 11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19
264 18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26
330 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30
396 30 31
396
462 July August September
528 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
594 1 2 3 1 2 3 4 5 6 7 1 2 3 4
660 4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11
next block
66 11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18
132 18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25
198 25 26 27 28 29 30 31 29 30 31 26 27 28 29 30
264
264
330 October November December
396 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
462 1 2 1 2 3 4 5 6 1 2 3 4
528 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11
594 10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18
660 17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25
next block
66 24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31
132 31
I have the problem to save each max 700 chars block into separate file - with following command it only produces one file.0, and expected were split files file.0, file.1, file.2 and file.3 for this input example
$ awk 'c=0;{l=length+l;if(l<=700){print>"file."c}else{c=c++;l=length;print>"file."c}}' sample_text
$ cksum *
3868619974 2180 file.0
3868619974 2180 sample_text
This should do it:
BEGIN {
maxChars = 700
out = "file.0"
}
{
numChars = length($0)
totChars += numChars
if ( totChars > maxChars ) {
close(out)
out = "file." ++cnt
totChars = numChars
}
print > out
}
// L is a list and n is its length //
// we assume that n= 4**k , for k≥1//
Alg1(L,n)
remove the smallest and largest element from L
if n-2 > (4**k)/2
call Alg1(L, n-2)
Not what it does but what is it intended to do? I don't understand what the question means by "intended" but I think the algorithm just removes the largest and smallest element of the list recursively until 4 or 3 elements remain.
Given a starting list of size 4^k, which appears to be implied by the definition given for n, alg1 reduces the size of the supplied list to ((4^k) / 2) + 2 for k >= 1. I agree with #Ctznkane525 that the algorithm is incompletely specified in that it doesn't tell us what the return value should be. But if we make the simple assumption that two elements should be removed from the end of the list each time n is decremented by 2 we can continue. Thus, consider the following implementation in Clojure:
(defn exp [x n]
(reduce * (repeat n x)))
(def k 1)
(defn alg1[l n]
(println "k=" k " n=" n " l=" l)
(if (> (- n 2) (/ (exp 4 k) 2))
(recur (take (- n 2) l) (- n 2))
l))
I've added code here to print the values of k, n, and l so we can watch what happens at each step.
Given the above we'll start a little testing. We'll invoke alg1 as (alg1 (take (exp 4 k) (iterate #(+ 1 %) 1)) (exp 4 k)), which simply creates a list of 4^k elements and passes it as the first argument to alg1, and passes 4^k for the second argument. So here goes:
user=> (def k 1)
#'user/k
user=> (alg1 (take (exp 4 k) (iterate #(+ 1 %) 1)) (exp 4 k))
k= 1 n= 4 l= (1 2 3 4)
(1 2 3 4)
So with k=1 and the list defined as (1 2 3 4) the function returns immediately, because n-2 = 2, and that's less than or equal to (4^k)/2, which is also 2.
Let's try with k=2:
user=> (def k 2)
#'user/k
user=> (alg1 (take (exp 4 k) (iterate #(+ 1 %) 1)) (exp 4 k))
k= 2 n= 16 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)
k= 2 n= 14 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14)
k= 2 n= 12 l= (1 2 3 4 5 6 7 8 9 10 11 12)
k= 2 n= 10 l= (1 2 3 4 5 6 7 8 9 10)
(1 2 3 4 5 6 7 8 9 10)
Ah, that's a bit more interesting. We start with n=16, which is of course 4^k = 4^2 = 16, and the beginning list is (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16). When these values are considered by alg1 it finds that n-2 (14) is greater than (4^2)/2 (8), so it trims two elements from the end of the list and recursively invokes itself. On the second iteration it finds that n-2 (12) is greater than 8 so it trims another two elements and recursively invokes itself. This continues until n=10, when alg1 finds that n-2 (8) is no longer greater than (4^2)/2 (8), so it returns the list (1 2 3 4 5 6 7 8 9 10).
What happens with k=3?
user=> (def k 3)
#'user/k
user=> (alg1 (take (exp 4 k) (iterate #(+ 1 %) 1)) (exp 4 k))
k= 3 n= 64 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64)
k= 3 n= 62 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62)
k= 3 n= 60 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60)
k= 3 n= 58 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58)
k= 3 n= 56 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56)
k= 3 n= 54 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54)
k= 3 n= 52 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52)
k= 3 n= 50 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50)
k= 3 n= 48 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48)
k= 3 n= 46 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46)
k= 3 n= 44 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44)
k= 3 n= 42 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42)
k= 3 n= 40 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40)
k= 3 n= 38 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38)
k= 3 n= 36 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36)
k= 3 n= 34 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34)
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34)
Similar results to the above. At each iteration two elements are trimmed from the list until the condition specified in the algorithm is reached, at which point the algorithm exits.
You can continue bumping up the value of k, building the arguments, and watching the algorithm work, but in the end the results are always similar: the list is reduced in size to ((4^k) / 2) + 2.
Best of luck.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Do we have any library or any mechanism where we can use true 7 byte key for DES instead of 8 byte key. I need it for keys analysis in DES and 8 byte key requirement for associated library is creating problem in getting actual keys analysis.
As explained in http://en.wikipedia.org/wiki/Data_Encryption_Standard, the 8-byte key for DES is just a 56-bit key in 8 bytes with odd parity check added:
The key is nominally stored or transmitted as 8 bytes, each with odd parity. According to ANSI X3.92-1981, section 3.5:
One bit in each 8-bit byte of the KEY may be utilized for error detection in key generation, distribution, and storage. Bits 8, 16,..., 64 are for use in ensuring that each byte is of odd parity.
(end quote)
So given an 8-byte key I can generate a 7-byte key by dropping the parity check bits and reformatting, and given a 7-byte key I can generate an 8-byte key by reformatting and adding parity check bits. It should therefore be easy to produce wrappers to make a library for one key format look like a library for another - or are you having some other problem I haven't noticed?
In DES a key is comprised of 8 bytes with the LSB a parity bit, a nice and regular structure. There's an implication there for the relationship between input bytes and Permuted Choice 1, which loads the two 28 bit C and D Registers.
In parlance of the NBS standard a Permuted Choice is a selection permutation not using all the values of a greater whole.
Historically any DES implementation that used 7 bytes wasn't compatible where there are several FIPS/Nist pubs specifying key/ciphertext/plaintext triplets.
If you have a need for a 56 bit number representing a key there's an implication you're using or storing them in a tabular fashion. Otherwise you could simply guarantee the parity bit is either correct or say '0'. 'Packed' keys are only of interest for saving storage space.
All that said and done if you need a 56 bit number representing a key you could represent 8 bytes of key in a 64 bit value and after identifying the parity bit locations with respect to endian-ness, shift the 64 bit array value obliterating the parity bits one at a time in 8 operations on a 64 bit machine, leaving 56 'effective' key bits.
On a 32 bit or smaller machine you'd also have to track byte position to keep track of bit shift offsets and would have to deal with bits moving between bytes to pack bits into 7 bytes.
For a 32 bit machine you could pack two 32 bit values into 28 bits in opposite directions then merge the proper 4 bits of the second one into the first followed by shifting the second value down 8 bits.
For a big-endian bit in byte numbering system 1 to 8 with bit 8 the LSB (from the standard)
Bits 1-4 go to the C register, with only four bit 4 values from 8 successive bytes of an input key (described as an input array of bits). The D block shares bit 4 and uses bits 5-7 to derived 28 bits:
The bigger issue here may be the ability to communicate any interesting finding in terms of keys useful to those dealing with an 8 byte key representation. It may be handy to have an inverse function available as well.
There's also a relationship between round keys and the C and D concatenated block, shown in Carl Meyers and Stephen Metyas book "Cryptography, A New Dimension in Computer Security', subtitled 'A Guide for the Design and Implementation of Secure Systems', Wiley Interscience, 1982, ISBN-0-471- 04892-5.
I recreated the table using a derivative of the original BSD libcrypt source. The significance of this is that C and D bits don't mix in the two 24 bit values derived from Permuted Choice 2 (which is visible in selected key (KS) in round 16 of the table).
Bit 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
KS
1 15 18 12 25 2 6 4 1 16 7 22 11 24 20 13 5 27 9 17 8 28 21 14 3
2 16 19 13 26 3 7 5 2 17 8 23 12 25 21 14 6 28 10 18 9 1 22 15 4
3 18 21 15 28 5 9 7 4 19 10 25 14 27 23 16 8 2 12 20 11 3 24 17 6
4 20 23 17 2 7 11 9 6 21 12 27 16 1 25 18 10 4 14 22 13 5 26 19 8
5 22 25 19 4 9 13 11 8 23 14 1 18 3 27 20 12 6 16 24 15 7 28 21 10
6 24 27 21 6 11 15 13 10 25 16 3 20 5 1 22 14 8 18 26 17 9 2 23 12
7 26 1 23 8 13 17 15 12 27 18 5 22 7 3 24 16 10 20 28 19 11 4 25 14
8 28 3 25 10 15 19 17 14 1 20 7 24 9 5 26 18 12 22 2 21 13 6 27 16
9 1 4 26 11 16 20 18 15 2 21 8 25 10 6 27 19 13 23 3 22 14 7 28 17
10 3 6 28 13 18 22 20 17 4 23 10 27 12 8 1 21 15 25 5 24 16 9 2 19
11 5 8 2 15 20 24 22 19 6 25 12 1 14 10 3 23 17 27 7 26 18 11 4 21
12 7 10 4 17 22 26 24 21 8 27 14 3 16 12 5 25 19 1 9 28 20 13 6 23
13 9 12 6 19 24 28 26 23 10 1 16 5 18 14 7 27 21 3 11 2 22 15 8 25
14 11 14 8 21 26 2 28 25 12 3 18 7 20 16 9 1 23 5 13 4 24 17 10 27
15 13 16 10 23 28 4 2 27 14 5 20 9 22 18 11 3 25 7 15 6 26 19 12 1
16 14 17 11 24 1 5 3 28 15 6 21 10 23 19 12 4 26 8 16 7 27 20 13 2
Bit 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
KS
1 42 53 32 38 48 56 31 41 52 46 34 49 45 50 40 29 35 54 47 43 51 37 30 33
2 43 54 33 39 49 29 32 42 53 47 35 50 46 51 41 30 36 55 48 44 52 38 31 34
3 45 56 35 41 51 31 34 44 55 49 37 52 48 53 43 32 38 29 50 46 54 40 33 36
4 47 30 37 43 53 33 36 46 29 51 39 54 50 55 45 34 40 31 52 48 56 42 35 38
5 49 32 39 45 55 35 38 48 31 53 41 56 52 29 47 36 42 33 54 50 30 44 37 40
6 51 34 41 47 29 37 40 50 33 55 43 30 54 31 49 38 44 35 56 52 32 46 39 42
7 53 36 43 49 31 39 42 52 35 29 45 32 56 33 51 40 46 37 30 54 34 48 41 44
8 55 38 45 51 33 41 44 54 37 31 47 34 30 35 53 42 48 39 32 56 36 50 43 46
9 56 39 46 52 34 42 45 55 38 32 48 35 31 36 54 43 49 40 33 29 37 51 44 47
10 30 41 48 54 36 44 47 29 40 34 50 37 33 38 56 45 51 42 35 31 39 53 46 49
11 32 43 50 56 38 46 49 31 42 36 52 39 35 40 30 47 53 44 37 33 41 55 48 51
12 34 45 52 30 40 48 51 33 44 38 54 41 37 42 32 49 55 46 39 35 43 29 50 53
13 36 47 54 32 42 50 53 35 46 40 56 43 39 44 34 51 29 48 41 37 45 31 52 55
14 38 49 56 34 44 52 55 37 48 42 30 45 41 46 36 53 31 50 43 39 47 33 54 29
15 40 51 30 36 46 54 29 39 50 44 32 47 43 48 38 55 33 52 45 41 49 35 56 31
16 41 52 31 37 47 55 30 40 51 45 33 48 44 49 39 56 34 53 46 42 50 36 29 32
This almost says your 56 bit number should be concatenated from the C and D Register values so there's a discernible relationship with round keys while allowing you to index based on C and/or D values.
I tried editing pyDes library code I just took 7 byte key and converted back to 64 bits by padding '0' to every multiple of 8th positions of bits. so the analysis on 7 byte-key actually required for algorithm hopefully is gained and the the parity bits are taken as 0 (that is not associated to my key now) :) . please comment if my key still is not solely for DES algorithm..
I am trying to learn bash at a deeper level, and I decided to make a multiplication table. I have the functionality with the statement :
echo $[{1..10}*{1..10}]
but that gives me the following output:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Is there any way to format this output like the following using only 1 statement (i can figure out how to do this with loops, but that's no fun :p )
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Is it even possible to do in one statement, or would I have to loop?
Use this line for a nice output without using loops:
echo $[{1..10}*{1..10}] | xargs -n10 | column -t
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Update
As a logical next step, I asked here if this multiplication table can have a variable range. With this help, my answer works with a variable ($boundary) range and stays quite readable:
boundary=4; eval echo $\[{1..$boundary}*{1..$boundary}\] | xargs -n$boundary | column -t
Output:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Also note that the $[..] arithmetic notation is deprecated and $((...)) should be used instead:
boundary=4; eval eval echo "$\(\({1..$boundary}*{1..$boundary}\)\)" | xargs -n$boundary | column -t
The printf built-in repeats its format as many times as necessary to print all arguments, so:
printf '%d %d %d %d %d %d %d %d %d %d\n' $[{1..10}*{1..10}]
If you want to avoid repeating the %d bit, it's trickier.
printf "$(echo %$[{1..10}*0]d)\\n" $[{1..10}*{1..10}]
In production code, use a loop.