Shard a collection in mongo atlas - database

Is it possible to Shard a collection in MongoDB atlas? I tried to Shard a collection but when going to enable sharding to my database it gave this error.
MongoServerError: (Unauthorized) not authorized on admin to execute command { enableSharding: "xapi", lsid: { id: {4 [5 97 193 191 78 207 71 39 186 1 57 136 181 234 80 85]} }, $clusterTime: { clusterTime: {1661827468 9}, signature: { hash: {0 [239 144 235 226 101 40 124 83 122 213 201 45 199 157 206 184 76 44 96 38]}, keyId: 7103402705267195904.000000 } }, $db: "admin" }
I am using MongoDB free tier (M0). and already allow permission to enable sharding. I attach the image
permission

Related

My code for "Count only Repeated" is not working. Have written clean code(according to me) that is easy to understand

Here is the question : https://www.geeksforgeeks.org/count-of-only-repeated-element-in-a-sorted-array-of-consecutive-elements/
Here is my code :
public static Point findRepeating(Integer arr[],int n)
{
// Point(return type) is a class having first and second as data memebres where first is number
// repeating and second is number of times it is repeating
if(arr[0] == arr[n-1]) // whole array has only one number
return new Point(arr[0],n);
int low = 0,high = n-1,mid = (low+high)/2, repNumber = 0;
while(high-low > 1) // in this loop only I'm trying to find the number that is repeating
{
mid = (low+high)/2;
if(arr[mid] == arr[mid-1] || arr[mid] == arr[mid+1])
{
repNumber = arr[mid];
break;
}
if((arr[low]+(mid-low)) == arr[mid])
low = mid+1;
else
high = mid;
}
int startIndex=0,endIndex=0;
// now I'll find the start and end index of the repeating number
// doing this by finding number just smaller and just larger than repeating number
if(repNumber == arr[0])
{
startIndex = 0;
endIndex = Arrays.binarySearch(arr,repNumber+1);
return new Point(repNumber,endIndex);
}
if(repNumber == arr[n-1])
{
endIndex = n-1;
startIndex = Arrays.binarySearch(arr,repNumber-1);
return new Point(repNumber,endIndex-startIndex);
}
else
{
startIndex = Arrays.binarySearch(arr,repNumber-1) + 1;
endIndex = Arrays.binarySearch(arr,repNumber+1);
return new Point(repNumber,endIndex-startIndex);
}
}
The test case where my code fails is(whole input is not visbile as it's huge):
Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:
28566
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 15.................
Its Correct output is:
16932 10086
And Your Code's output is:
0 0
This question can be simply solved using Linear Search within Time Complexity less than O(N) ( O(N) in worst case ).
In first step we can check whether the first and last element of array are equal or not?
If Yes,
We can return the size of array as our answer.
Otherwise we can check Linearly using two methods
Method 1 : We can take the consecutive Differences between two Numbers and check whether they give us Zero as result or not
int count = 0;
vector<int>arr = { 1,2,2,2,2,2,2,3 };
for(int i = 0; i < arr.size() ; i++){
if(arr[i+1] - arr[i] == 0 ){
count++;
}
}
Method 2 : By Taking XOR of two consecutive elements of array and When XOR is zero increment the count
int count = 0;
vector<int>arr = { 1,2,2,2,2,2,2,3 };
for(int i = 0; i < arr.size() ; i++){
if((arr[i+1]^arr[i]) == 0 ){
count++;
}
}

Haproxy header differs from what is expected

I am working with Haproxy and need to read the v2 header from TCP stream, according to spec it is supposed to have first 13 bytes following:
\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A\x02
in byte
{byte[13]}
[0]: 13
[1]: 10
[2]: 13
[3]: 10
[4]: 0
[5]: 13
[6]: 10
[7]: 81
[8]: 85
[9]: 73
[10]: 84
[11]: 10
[12]: 2
however I get
[0]: 13
[1]: 10
[2]: 0
[3]: 13
[4]: 10
[5]: 81
[6]: 85
[7]: 73
[8]: 84
[9]: 10
[10]: 33
[11]: 17
[12]: 0
My read does seem to be missing first two bytes 13 10 from the spec, however ending seems to differ too, I can't seem to figure out why...
What is even more confusing I can't see from IP address (54.219.177.232) nor to (45.32.136.69) nowhere in the header they are supposed to be 17th byte onward
Count = 91
[0]: 12
[1]: 180
[2]: 183
[3]: 211
[4]: 173
[5]: 172
[6]: 31
[7]: 23
[8]: 237
[9]: 228
[10]: 70
[11]: 1
[12]: 187
[13]: 22
[14]: 3
[15]: 1
[16]: 0
[17]: 178
[18]: 1
[19]: 0
[20]: 0
[21]: 174
[22]: 3
[23]: 3
[24]: 65
[25]: 71
[26]: 172
[27]: 83
[28]: 43
[29]: 146
[30]: 158
[31]: 30
[32]: 150
[33]: 85
[34]: 84
[35]: 34
[36]: 158
[37]: 111
[38]: 242
[39]: 247
[40]: 173
[41]: 198
[42]: 13
[43]: 133
[44]: 9
[45]: 39
[46]: 111
[47]: 9
[48]: 44
[49]: 64
[50]: 232
[51]: 25
[52]: 14
[53]: 51
[54]: 110
[55]: 76
[56]: 0
[57]: 0
[58]: 32
[59]: 74
[60]: 74
[61]: 192
[62]: 43
[63]: 192
[64]: 47
[65]: 192
[66]: 44
[67]: 192
[68]: 48
[69]: 204
[70]: 169
[71]: 204
[72]: 168
[73]: 204
[74]: 20
[75]: 204
[76]: 19
[77]: 192
[78]: 19
[79]: 192
[80]: 20
[81]: 0
[82]: 156
[83]: 0
[84]: 157
[85]: 0
[86]: 47
[87]: 0
[88]: 53
[89]: 0
[90]: 10`
Code that reads header looks following:
var childSocketThread = new Thread(() =>
{
var getOrPostHttp1Acc = new List<byte>();
var getOrPostHttp1 = new byte[1];
while (getOrPostHttp1[0] != 10)
{
socket.Receive(getOrPostHttp1);
getOrPostHttp1Acc.Add(getOrPostHttp1[0]);
}
bool isGet = Encoding.ASCII.GetString(getOrPostHttp1Acc.ToArray()).ToUpper().Contains("GET");
byte[] proxyv2headerIdentifier = new byte[13];
socket.Receive(proxyv2headerIdentifier);
var proxyv2header = new List<byte>();
var proxyv2HeaderBuffer = new byte[1];
if(proxyv2headerIdentifier.SequenceEqual(proxyv2HeaderStartRequence))//TODO:uncomment this, hardcoded (true) is only for testing
{
while (proxyv2HeaderBuffer[0] != 10)
{
socket.Receive(proxyv2HeaderBuffer);
proxyv2header.Add(proxyv2HeaderBuffer[0]);
}
string headerString = $"proxyv2:{Encoding.ASCII.GetString(proxyv2header.ToArray())}";
byte[] bodyBuff = new byte[0];
byte[] buffer = new byte[1];
int contentLength = 0;
while (!headerString.Contains("\r\n\r\n"))
//at this point header is read into proxyv2header ....
Where am I misreading specification, or doing something wrong?
Ok I have figured this one myself...
mistake was to read the http method first (if it's GET or POST) haproxy does not start with that... so removing method read
var getOrPostHttp1Acc = new List<byte>();
var getOrPostHttp1 = new byte[1];
while (getOrPostHttp1[0] != 10)
{
socket.Receive(getOrPostHttp1);
getOrPostHttp1Acc.Add(getOrPostHttp1[0]);
}
bool isGet = Encoding.ASCII.GetString(getOrPostHttp1Acc.ToArray()).ToUpper().Contains("GET");
and reducing start sequence to 12 bytes did it...
In regards to IP, it was hiding in the plain sight all this time...
(check bytes 4-7)
Hope this saves you some time.

R About Array replacement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
ordered_use
2 UNIT CONVERTED DWELLING
28706 51
2 UNIT DWELLING 2 UNITS
99 44
3 UNIT DWELLING APARTMENT
31 4733
APARTMENT APARTMENT BLDG
38 37
APARTMENT BUILDING APARTMENT UNIT
2042 37
APPARTMENT BUILDING APT
54 357
APT BLDG APT BUILDING
78 49
APT. APT. BLDG
41 61
APT. BUILDING ARENA
35 67
BANK BOWLING ALLEY
302 267
BUNGALOW CAR DEALERSHIP
85 62
CHURCH CLUB
94 40
COLLEGE COMMERCIAL
196 410
COMMERCIAL/RESIDENTIAL COMMUNITY CENTRE
56 131
COMMUNITY HALL CONDO
31 223
CONDOMINIUM CONVERTED DWELLING
42 42
CONVERTED HOUSE CONVERTED HOUSE - 2 UNITS
149 124
CONVERTED HOUSE - 3 UNITS CONVERTED HOUSE (2 UNITS)
56 35
CONVERTED HOUSE 2 UNITS CONVERTED HOUSE, 2 UNITS
38 84
CONVERTED HOUSE, 3 UNITS DAYCARE
42 31
DENTAL OFFICE DETACHED - SFD
87 513
DETACHED - SINGLE FAMILY DWELLING DETACHED HOUSE
97 130
DETACHED SFD DUPLEX
190 145
ELEMENTARY SCHOOL FIRE HALL
859 41
FITNESS CENTRE FUNERAL HOME
48 36
GARAGE GAS STATION
63 130
GROCERY STORE GROUP HOME
51 45
HAIR SALON HOME FOR THE AGED
46 49
HOSPITAL HOTEL
971 215
HOUSE IND
1249 219
INDUSTRIAL INDUSTRIAL
1725 35
INDUSTRIAL BUILDING INDUSTRIAL MANUFACTURING
51 91
INDUSTRIAL WAREHOUSE INSTITUTIONAL
61 48
LAB LABORATORY
56 46
LIBRARY LONG TERM CARE FACILITY
91 74
LUMBER YARD MANUFACTURING
53 55
MEDICAL OFFICE MIXED USE
247 539
MIXED USE MIXED USE (COMMERCIAL)
40 34
MIXED USE (RETAIL) MIXED USE BUILDING
74 297
MIXED USE BUILDING/NON RESIDENTIAL MIXED USE NON RES
37 93
MIXED USE NON RES (RETAIL) MIXED USE RES & NON RES
52 59
MIXED-USE MULTI UNIT
202 54
MULTI UNIT BUILDING MULTI USE
70 381
MULTI USE, NON RES MULTI USE/NON RES
134 36
MULTI USE/NON RESIDENTIAL MULTIPLE UNIT
49 149
MULTIPLE UNIT BUILDING MUSEUM
40 40
N/A NONE
650 264
NOT KNOWN NURSING HOME
58 55
OFF OFFICE
181 9698
OFFICE OFFICE BLD
50 46
OFFICE BUILDING OFFICE SPACE
177 39
OFFICE/RETAIL OFFICE/WAREHOUSE
36 95
OFFICES OTHER
63 54
PARK PARKING GARAGE
137 149
PARKING LOT PERSONAL SERVICE SHOP
126 49
PLACE OF WORSHIP POLICE STATION
516 34
PROF. OFFICE RECREATIONAL
65 46
REPAIR GARAGE RES
91 242
RESIDENTIAL RESIDENTIAL - SFD
488 561
RESIDENTIAL CONDO REST
39 46
RESTAURANT RESTAURANT > 30 SEATS
1074 42
RESTAURANT GREATER THAN 30 SEATS RESTAURANT LESS THAN 30 SEATS
145 69
RESTAURANT UNDER 30 SEATS RESTAURANT, GREATER THAN 30 SEATS
47 42
RET RETAIL
81 4001
RETAIL RETAIL MALL
46 61
RETAIL PLAZA RETAIL STORE
96 796
RETAIL/OFFICE RETAIL/RESIDENTIAL
32 99
ROOMING HOUSE ROW HOUSE
89 38
SCHOOL SECONDARY SCHOOL
594 246
SEMI SEMI DETACHED
209 218
SEMI DETACHED - SFD SEMI DETACHED - SINGLE FAMILY DWELLING
212 50
SEMI DETACHED SFD SEMI-DETACHED
46 71
SEMI-DETACHED - SFD SEMI-DETACHED DWELLING
241 172
SEMI-DETACHED HOUSE SEMI-DETACHED SFD
56 155
SEMI-DETACHED SINGLE FAMILY DWELLING SFD
90 26479
SFD - DETACHED SFD - DETCAHED
3817 79
SFD - ROWHOUSE SFD - SEMI
76 206
SFD - SEMI DETACHED SFD - SEMI-DETACHED
353 209
SFD - SEMIDETACHED SFD - TOWNHOUSE
158 131
SFD DET SFD DETACEHD
495 39
SFD DETACHED SFD DETATCHED
755 231
SFD ROWHOUSE SFD SEMI
31 857
SFD SEMI DETACHED SFD SEMI-DETACHED
59 167
SFD TOWNHOUSE SFD-DETACHED
155 8148
SFD-DETACHED SFD-ROWHOUSE
37 56
SFD-SEMI SFD-SEMI DETACHED
1189 613
SFD-SEMI-DETACHED SFD-TOWNHOUSE
313 526
SINGLE SINGLE FAMILY
41 64
SINGLE FAMILY DETACHED SINGLE FAMILY DETACHED DWELLING
1615 222
SINGLE FAMILY DETACHED HOUSE SINGLE FAMILY DWELLING
58 2673
SINGLE FAMILY SEMI-DETACHED SINGLE-FAMILY DETACHED HOUSE
54 107
SINGLE-FAMILY SEMI-DETACHED HOUSE STADIUM
53 37
STUDENT RESIDENCE SUBWAY STATION
34 44
SURFACE PARKING LOT/EXISTING COMMERCIAL BUILDING TAKE OUT RESTAURANT
57 34
THEATRE TOWNHOUSE
38 198
TOWNHOUSE - SFD TOWNHOUSES
97 31
TRANSIT STATION TRIPLEX
70 54
UNION STATION UNIVERSITY
52 359
UNIVERSITY OF TORONTO VACANT
42 15010
VACANT VACANT (AFTER DEMO)
77 36
VACANT COMMERCIAL VACANT COMMERCIAL UNIT
37 63
VACANT INDUSTRIAL VACANT LAND
32 1107
VACANT LOT VACANT RETAIL
447 112
VACANT RETAIL UNIT VACANT SINGLE FAMILY DWELLING
46 82
VACANT SPACE VACANT UNIT
120 117
VACNT WAREHOUSE
42 526
WAREHOUSE/OFFICE WATER TREATMENT PLANT
54 46
Apartment <- (ordered_use[6]+ ordered_use[7]+ ordered_use[8] + ordered_use[9] + ordered_use[10] + ordered_use[11] + ordered_use[12] + ordered_use[13] + ordered_use[14] + ordered_use[15] + ordered_use[16] + ordered_use[17] + ordered_use[30] + ordered_use[31] + ordered_use[33] + ordered_use[34] + ordered_use[35] + ordered_use[36] + ordered_use[37] + ordered_use[38] + ordered_use[39] + ordered_use[84] + ordered_use[85] + ordered_use[90] + ordered_use[91])
I am trying to convert anything that looks like an apartment,building,condo, unit and etc therefore I combined everything which looks similar but my question is, how can I replace those with my combined data of Apartment
To get something to work with I pasted your text into the space between the quotes of:
ordered_use <- read.fwf(textConnection("___"), widths=c(50,50), stringsAsFactors=FALSE)
And then trimmed blank-space and extracted every other row of the odd items and applied as.numeric to the even rows>
ordered_use[] <- lapply(ordered_use, trim)
ord2 <- data.frame(
nams <- c( ordered_use[ c(TRUE,FALSE), "V1"], ordered_use[ c(TRUE,FALSE), "V2"]),
nums=as.numeric(c( ordered_use[ c(FALSE,TRUE), "V1"], ordered_use[ c(FALSE,TRUE), "V2"]) )
> head(ord2)
nams nums
1 28706
2 2 UNIT DWELLING 99
3 3 UNIT DWELLING 31
4 APARTMENT 38
5 APARTMENT BUILDING 2042
6 APPARTMENT BUILDING 54
To extract items with "APT" or "CONDO" use grepl
> ord2[ grepl("APART|APPART|APT|CONDO", ord2$nams) , ]
nams nums
4 APARTMENT 38
5 APARTMENT BUILDING 2042
6 APPARTMENT BUILDING 54
7 APT BLDG 78
8 APT. 41
9 APT. BUILDING 35
16 CONDOMINIUM 42
60 RESIDENTIAL CONDO 39
110 APARTMENT 4733
111 APARTMENT BLDG 37
112 APARTMENT UNIT 37
113 APT 357
114 APT BUILDING 49
115 APT. BLDG 61
122 CONDO 223
I cannot tell whether your item numbers match up since you probably have a table object and I have two columns that are not arranges the same as yours.
> sum( ord2[ grepl("APART|APPART|APT|CONDO", ord2$nams) ,"nums" ])
[1] 7866
You should post the output of dput(head(ordered_use, 20)) if you want an answer tailored to the type of object you have.

CipherSaber bug

So I implemented ciphersaber-1. It almost works, I can decrypt the cstest1.cs1. But i have trouble getting cstest2.cs1 to work.
The output is:
The Fourth Amendment to the Constitution of the Unite ▀Stat→s of America
"The right o☻ the people to be secure in their persons, houses, papers, and
effects, against unreasonab→e searches an╚A)┤Xx¹▼☻dcðþÈ_#­0Uc.?n~J¿|,lómsó£k░7╠▄
íuVRÊ ╣├xð"↕(Gû┤.>!{³♫╚Tƒ}Àõ+»~C;ÔÙ²÷g.qÏø←1ß█yÎßsÈ÷g┐ÅJÔÞ┘Îö║AÝf╔ìêâß╗È;okn│CÚê
õ&æÄ[5&Þ½╔s╦Nå1En♂☻♫ôzÓ9»Á╝ÐÅ├ðzÝÎòeØ%W¶]¤▲´Oá╗e_Ú)╣ó0↑ï^☻P>ù♂­¥¯▄‗♦£mUzMצվ~8å
ì½³░Ùã♠,H-tßJ!³*²RóÅ
So I must have a bug in initializing the state. The odd thing is that I can encrypt and decrypt long texts without problems, so the bug is symmetric.
I implemented the rc4 cipher as a reentrent single byte algorithm as you can see in rc4.c.
The state is stored in the rc4_state struct:
typedef unsigned char rc4_byte;
struct rc4_state_
{
rc4_byte i;
rc4_byte j;
rc4_byte state[256];
};
typedef struct rc4_state_ rc4_state;
The state is initialized with rc4_init:
void rc4_init(rc4_state* state, rc4_byte* key, size_t keylen)
{
rc4_byte i, j, n;
i = 0;
do
{
state->state[i] = i;
i++;
}
while (i != 255);
j = 0;
i = 0;
do
{
n = i % keylen;
j += state->state[i] + key[n];
swap(&state->state[i], &state->state[j]);
i++;
}
while (i != 255);
state->i = 0;
state->j = 0;
}
The actual encryption / decryption is done in rc4:
rc4_byte rc4(rc4_state* state, rc4_byte in)
{
rc4_byte n;
state->i++;
state->j += state->state[state->i];
swap(&state->state[state->i], &state->state[state->j]);
n = state->state[state->i] + state->state[state->j];
return in ^ state->state[n];
}
For completeness, swap:
void swap(rc4_byte* a, rc4_byte* b)
{
rc4_byte t = *a;
*a = *b;
*b = t;
}
I have been breaking my head on this for more than two days... The state, at least for the "asdfg" key is correct. Any help would be nice.
The whole thing can be found in my github reopsitory: https://github.com/rioki/ciphersaber/
I stumbled across your question while searching online, but since you haven't updated your code at GitHub yet, I figured you might still like to know what the problem was.
It's in this bit of code:
i = 0;
do
{
state->state[i] = i;
i++;
}
while (i != 255);
After this loop has iterated 255 times, i will have a value of 255 and the loop will terminate. As a result, the last byte of your state buffer is being left uninitialised.
This is easily fixed. Just change while (i != 255); to while (i);.
Sorry you haven't gotten feedback, I finally pulled this off in Python 3 today, but don't know enough about C to debug your code.
Some of the links on the main ciphersaber page are broken (pointing to ".com" instead of ".org"), so you might not have found the FAQ:
http://ciphersaber.gurus.org/faq.html
It includes the following debugging tips:
Make sure you are not reading or writing encrypted files as text files. You must use binary mode for file I/O.
If you are writing in the C language, be sure to store bytes as unsigned char.
Watch out for classic indexing problems. Do arrays in you chosen programming language start with 0 or 1?
Make sure you are writing out a random 10 byte IV when you encrypt and are reading the IV from the start of the file when you decrypt.
If your program still does not work, put in some statements to print out the S array after the key setup step. Then run your program to
decrypt the file cstest1.cs1 using asdfg as the key. Here is how the S
array should look:
file: cstest1.cs1
key: asdfg
176 32 49 160 15 112 58 8 186 19 50 161 60 17 82 153 37 141 131 127 59
2 165 103 98 53 9 57 41 150 174 64 36 62 191 154 44 136 149 158 226
113 230 227 247 155 221 34 125 20 163 95 128 219 1 181 201 146 88 204
213 80 143 164 145 234 134 248 100 77 188 235 76 217 194 35 75 99 126
92 243 177 52 180 83 140 198 42 151 18 91 33 16 192 101 48 97 220 114
110 124 72 139 218 142 118 81 84 31 29 195 68 209 172 200 214 93 240
61 22 206 123 152 7 203 10 119 171 79 250 109 137 199 167 11 104 211
129 208 216 178 207 242 162 30 120 65 115 87 170 47 69 244 212 45 85
73 222 225 185 63 0 179 210 108 245 202 46 96 148 51 173 24 182 89 116
3 67 205 94 231 23 21 13 169 215 190 241 228 132 252 4 233 56 105 26
12 135 223 166 238 229 246 138 239 54 5 130 159 236 66 175 189 147 193
237 43 40 117 157 86 249 74 27 156 14 133 251 196 187 197 102 106 39
232 255 121 122 253 111 90 38 55 70 184 78 224 25 6 107 168 254 144 28
183 71
I also found the "memorable test cases" helpful here:
http://www.cypherspace.org/adam/csvec/
Including:
key="Al"+ct="Al Dakota buys"(iv="Al Dakota "):
pt = "mead"
Even though the memorable test cases require cs2, upgrading to cs2 from cs1 is fairly trivial, you may be able to confidently convert your program to cs2 from cs1 even without fully debugging the rest of it.
Also note that the FAQ claims there used to be a file on the site that wouldn't decode, make sure your target file doesn't begin with "0e e3 f9 b2 40 11 fc 3e ..."
(Though I think that was a smaller test file, not the certificate.)
Oh, and also know that the site's not really up to date on the latest research into RC4 and derivatives. Just reserve this as a toy program unless all else fails.
Python
Here's one I wrote in Python for a question that later got deleted. It processes the file as a stream so memory usage is modest.
Usage
python encrypt.py <key> <rounds> < <infile> > <outfile>
python decrypt.py <key> <rounds> < <infile> > <outfile>
rc4.py
#!/usr/bin/env python
# coding: utf-8
import psyco
from sys import stdin,stdout,argv
def rc4(K):
R=range(256)
S=R[:]
T=bytearray(K*256)[:256]
j=0
for i in R*int(argv[2]):
j=j+S[i]+T[i]&255
S[i],S[j]=S[j],S[i]
i=j=0
while True:
B=stdin.read(4096)
if not B: break
for c in B:
i+=1&255
j=j+S[i]&255
S[i],S[j]=S[j],S[i]
stdout.write(chr(ord(c)^S[S[i]+S[j]&255]))
psyco.bind(rc4)
encrypt.py
from rc4 import *
import os
V=os.urandom(10)
stdout.write(V)
rc4(argv[1]+V)
decrypt.py
from rc4 import *
V=stdin.read(10)
rc4(argv[1]+V)

Read file with strings and integers in matlab

I have to read a file in Matlab that looks like this:
D:\Classified\positive-videos\vid.avi 163 3 14 32 54 79 105 130 155 202 216 224 238 250 262 288 288 322 357 369 381 438 457 478 499 525 551
D:\Classified\positive-videos\vid2.avi 163 3 14 32 54 79 105 130 155 202 216 224 238 250 262 288 288 322 357 369 381 438 457 478 499 525 551
There are many such lines separated by newline. I need to read it such that: I discard path of video name and first integer(eg 163 in first line) and read rest all the numbers in an array till new line occurs. How can this be done?
You could do the following:
fid = fopen('test1.txt','r');
my_line = fgetl(fid);
while(my_line ~= -1)
my_array = regexp(my_line,' ','split');
my_line = fgetl(fid);
disp(my_array(3:end));
end
fclose(fid);
This would give you:
ans =
Columns 1 through 11
'3' '14' '32' '54' '79' '105' '130' '155' '202' '216' '224'
Columns 12 through 22
'238' '250' '262' '288' '288' '322' '357' '369' '381' '438' '457'
Columns 23 through 26
'478' '499' '525' '551'
ans =
Columns 1 through 11
'3' '14' '32' '54' '79' '105' '130' '155' '202' '216' '224'
Columns 12 through 22
'238' '250' '262' '288' '288' '322' '357' '369' '381' '438' '457'
Columns 23 through 26
'478' '499' '525' '551'
EDIT
For a numeric matrix result you can change it as:
clear;
close;
clc;
fid = fopen('test1.txt','r');
my_line = fgetl(fid);
my_array = regexp(my_line,' ','split');
my_matrix = zeros(0, numel(my_array(3:end)));
ii = 1;
while(my_line ~= -1)
my_array = regexp(my_line,' ','split');
my_line = fgetl(fid);
my_matrix = [my_matrix;zeros(1,size(my_matrix,2))];
for jj=1:numel(my_array(3:end))
my_matrix(ii,jj) = str2num(cell2mat(my_array(jj+2)));
end
ii = ii + 1;
end
fclose(fid);
This would yeild:
my_matrix =
3 14 32 54 79 105 130 155 202 216 224 238 250 262 288 288 322 357 369 381 438 457 478 499 525 551
3 14 32 54 79 105 130 155 202 216 224 238 250 262 288 288 322 357 369 381 438 457 478 499 525 551
A way easier method follows up:
fid = importdata(filename)
results = fid.data;
Ad maiora.
EDIT
Since you wanna discard the first value after the string, you will have to call
res = fid.data(:,2:end);
instead of results.

Resources