All language subtitles for 22. Exercise 5 - A program to calculate character appearances in a file - Solution

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian Download
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,840 --> 00:00:01,890 All right. 2 00:00:02,670 --> 00:00:09,510 So first of all, just before we move on, OK, I want you to go here, OK, and go to the project. 3 00:00:10,470 --> 00:00:11,500 Where it resides. 4 00:00:11,520 --> 00:00:16,140 OK, so you get this nice information about your project. 5 00:00:16,560 --> 00:00:20,320 So here I have created a file called my file. 6 00:00:21,600 --> 00:00:27,720 And I've placed here some information like, Hi, this is Vlad, how are you doing on my al-Fatah course? 7 00:00:27,790 --> 00:00:35,070 OK, and saved these file as these OK, so I know that there is a file called my file that DSD with 8 00:00:35,070 --> 00:00:36,600 some information inside of it. 9 00:00:37,350 --> 00:00:44,730 And these file will be mainly used for comparing and testing out whether the program I have written 10 00:00:44,970 --> 00:00:47,010 works fine or not. 11 00:00:47,430 --> 00:00:54,810 OK, so let's close it and start working on whatever we need to do in this exercise to solve it correctly. 12 00:00:55,890 --> 00:00:58,410 So we are going to read a couple of things. 13 00:00:58,410 --> 00:01:03,330 We are going to read a certain character and we are also going to read the file name itself. 14 00:01:03,960 --> 00:01:06,120 Basically, that's all that we need to do. 15 00:01:06,360 --> 00:01:06,690 OK? 16 00:01:06,720 --> 00:01:12,570 We assume that the file is going to reside in the same workspace, right? 17 00:01:12,720 --> 00:01:17,160 That's why we do not need to specify the full path to the file. 18 00:01:17,790 --> 00:01:21,110 And we will basically it will be enough. 19 00:01:21,120 --> 00:01:23,050 We're working just with the file name itself. 20 00:01:23,070 --> 00:01:23,430 OK. 21 00:01:24,330 --> 00:01:28,680 So first of all, let's create char desired character. 22 00:01:28,950 --> 00:01:35,100 OK, so desire character will be the actual character that we want to calculate and to see how many 23 00:01:35,100 --> 00:01:40,620 times this character appears in a certain file, like the file that we've just seen. 24 00:01:41,520 --> 00:01:46,990 Also, what we've got we are going to see is to create some, let's say, array of characters. 25 00:01:47,010 --> 00:01:48,740 Let's say of size. 26 00:01:48,810 --> 00:01:49,200 I don't know. 27 00:01:49,200 --> 00:01:49,740 20. 28 00:01:50,190 --> 00:01:56,760 And we will initialize it with zeros just to make sure that the last year there will be is zero value 29 00:01:57,060 --> 00:02:02,100 if we exceed the actual size of the file name itself. 30 00:02:02,400 --> 00:02:07,770 OK, so we created two variables are two one variable. 31 00:02:07,780 --> 00:02:10,350 These are character and also an array of characters. 32 00:02:10,710 --> 00:02:15,690 And what we are going to do now is simply say the following thing print. 33 00:02:17,070 --> 00:02:17,920 All its use. 34 00:02:17,940 --> 00:02:24,390 I don't know, please or answer these, I desired a desired character. 35 00:02:25,320 --> 00:02:29,820 So the user is going to specify a character and we are going to read this character. 36 00:02:30,210 --> 00:02:35,220 One of the options, look, I'm not going to go into it because it's not very important for now, OK? 37 00:02:36,620 --> 00:02:42,180 The best way to do that, but we are going to do it with a simple scan of percentage. 38 00:02:42,180 --> 00:02:43,620 See to read a character. 39 00:02:43,860 --> 00:02:52,020 And also, we are going to ask the user, enter a file name you want to test, OK? 40 00:02:53,340 --> 00:02:59,100 Now we are going to read the actual file name from the user, and we are going to read it using the 41 00:02:59,100 --> 00:03:05,160 percentage ask and store it, where where should we store it inside a file name itself? 42 00:03:06,030 --> 00:03:06,600 OK, so. 43 00:03:07,650 --> 00:03:14,190 We have read the information and store it in the file name, so now file name, we assume that it will 44 00:03:14,190 --> 00:03:18,720 have the proper file name of some file that already exists. 45 00:03:19,230 --> 00:03:19,590 Right? 46 00:03:20,280 --> 00:03:20,700 Awesome. 47 00:03:21,210 --> 00:03:27,690 So now what we need to do once we created it, we need to open the file itself. 48 00:03:27,690 --> 00:03:33,020 And if we want to open the file, we need probably to create these file pointer RFP. 49 00:03:33,630 --> 00:03:37,650 OK, a pointer to these file structure, right? 50 00:03:38,490 --> 00:03:42,330 And the way we open up a file is by using the FP. 51 00:03:43,370 --> 00:03:44,570 Equals to what? 52 00:03:44,850 --> 00:03:45,650 Two, if open. 53 00:03:46,370 --> 00:03:47,000 Of what? 54 00:03:47,180 --> 00:03:48,750 Of the file name itself. 55 00:03:48,770 --> 00:03:49,070 Right. 56 00:03:49,070 --> 00:03:55,610 Because the file name is basically a string containing the actual file name that we are interested in 57 00:03:55,610 --> 00:03:58,560 and we are going to open it for reading mode. 58 00:03:59,520 --> 00:03:59,850 Right. 59 00:04:00,450 --> 00:04:07,230 We are going also to ask if B does not equal to now, OK, just to make sure that the file was opened 60 00:04:07,230 --> 00:04:12,960 correctly, then what we are going to do is say what we need to do. 61 00:04:12,960 --> 00:04:20,580 Basically, guys, what do you think we need, first of all, to go over each and any one of the characters 62 00:04:20,580 --> 00:04:23,160 inside of this file? 63 00:04:23,190 --> 00:04:23,520 Right. 64 00:04:23,520 --> 00:04:25,650 So if that's our file, where is it? 65 00:04:26,190 --> 00:04:31,680 If that's our file, we need to read this character, read these character or these character and so 66 00:04:31,680 --> 00:04:32,310 on and so forth. 67 00:04:32,580 --> 00:04:35,760 Until right, we reach the end of the file. 68 00:04:36,420 --> 00:04:43,500 So on every kind of reading operation, we are going to read one character after the other and comparing 69 00:04:43,500 --> 00:04:45,810 it with the desire or character. 70 00:04:46,880 --> 00:04:52,790 And if the result is true, if they are the same, then we will need to count this. 71 00:04:53,000 --> 00:04:56,210 So for that, we need to create additional variable in college. 72 00:04:56,600 --> 00:04:58,070 I don't know counts. 73 00:04:58,070 --> 00:05:01,460 Let's go count to appearances equals to zero. 74 00:05:02,390 --> 00:05:03,770 OK, so far so good. 75 00:05:04,680 --> 00:05:05,190 Awesome. 76 00:05:05,610 --> 00:05:13,290 So if we said that we are going to read the file until we come to it and so if file and a file. 77 00:05:14,060 --> 00:05:19,340 For FBI, then what we need to do is ask a simple question if. 78 00:05:20,210 --> 00:05:26,240 F get C right, because we need to read from file one character, FBI. 79 00:05:27,270 --> 00:05:32,830 If it equals two, what do the desire and character, then we simply do. 80 00:05:32,850 --> 00:05:36,420 Count appearances, count appearances plus plus. 81 00:05:36,960 --> 00:05:37,710 That's it. 82 00:05:38,340 --> 00:05:46,980 So we are going to go over each of the characters under the file that we opened using the FP pointer 83 00:05:47,250 --> 00:05:50,550 and then to increment every time count appearances. 84 00:05:50,550 --> 00:05:57,180 Plus plus, if we found that these certain character that we read equals to the desire or the character 85 00:05:57,180 --> 00:05:58,320 that we are looking for. 86 00:05:59,010 --> 00:05:59,370 OK. 87 00:05:59,940 --> 00:06:05,460 Finally, what we are going to do inside of these program is to use the printer function and say the 88 00:06:05,460 --> 00:06:15,270 following like total appearances appearances of character, of which character of character percentage 89 00:06:15,270 --> 00:06:24,600 C. Let's say it will be percentage and we will specify the desired character here and the count appearances 90 00:06:25,170 --> 00:06:25,890 right here. 91 00:06:26,670 --> 00:06:27,210 All right. 92 00:06:27,660 --> 00:06:29,730 So that's basically what we are going to do. 93 00:06:31,450 --> 00:06:37,220 Now, what I suggest is let's test these function and these program, basically. 94 00:06:37,270 --> 00:06:40,210 Okay, so that's all of the program that we developed. 95 00:06:40,570 --> 00:06:43,010 Now let's test that and see how it works. 96 00:06:43,030 --> 00:06:45,650 OK, so let's say we have these file, right? 97 00:06:45,670 --> 00:06:53,980 This is my file, and I can see that I have a capital v Vlad just once inside of these files. 98 00:06:53,980 --> 00:06:58,840 So let's test and see if this program works pretty much as we expect it to. 99 00:06:59,110 --> 00:07:03,400 OK, so it's run it and we have a problem, of course. 100 00:07:03,850 --> 00:07:05,940 How can it be without problems? 101 00:07:05,950 --> 00:07:09,010 Let's try to figure out what happens on initialized. 102 00:07:09,620 --> 00:07:13,210 Who is in on a useless Conniff percentage rate? 103 00:07:13,540 --> 00:07:16,840 So here we use the ampersand, the specified the address. 104 00:07:17,290 --> 00:07:17,630 OK. 105 00:07:18,220 --> 00:07:24,370 Here in the file name, we do not use of the ampersand, since we already know that file name itself 106 00:07:24,370 --> 00:07:28,960 represents the address of the first element inside of this array of characters. 107 00:07:29,740 --> 00:07:36,520 So now we run this program and we are going to see enter a desired character and these character will 108 00:07:36,520 --> 00:07:37,870 be Capital V. 109 00:07:38,380 --> 00:07:42,190 Press Enter and now enter a file name you want to test. 110 00:07:42,430 --> 00:07:44,200 So the file name was my. 111 00:07:45,970 --> 00:07:47,510 My file, my file. 112 00:07:48,520 --> 00:07:48,880 Right. 113 00:07:49,790 --> 00:07:50,780 Let's check it out. 114 00:07:50,990 --> 00:07:56,690 So total appearances of character vs one, which is exactly as we thought. 115 00:07:57,290 --> 00:08:04,450 And let's try to run this program and see how it works with, let's say, character I presenters. 116 00:08:04,450 --> 00:08:12,320 So my file Dottie XD, oh, so total appearances of character I equals two four. 117 00:08:12,800 --> 00:08:14,600 Let's make sure that it actually the case. 118 00:08:14,600 --> 00:08:16,260 So we have one. 119 00:08:16,520 --> 00:08:20,960 We have two, three and four. 120 00:08:21,440 --> 00:08:23,210 Let's make sure that there are no others. 121 00:08:23,240 --> 00:08:23,660 Awesome. 122 00:08:24,110 --> 00:08:25,760 So four characters for I. 123 00:08:26,210 --> 00:08:29,990 And I think basically that you got the idea of how this program works. 124 00:08:30,860 --> 00:08:33,380 Hopefully, it works also on your side as well. 125 00:08:33,890 --> 00:08:36,830 And yeah, this is the end of these video. 126 00:08:37,040 --> 00:08:40,520 And guys, please feel free to use it however you like. 127 00:08:40,850 --> 00:08:47,150 Also, if you makes a little bit and you try to, first of all, get the file name itself. 128 00:08:47,750 --> 00:08:54,560 So if you're using the basic scanning function, make sure that you use your space before the percentage. 129 00:08:54,560 --> 00:09:00,410 See, because if you do it like this, then whenever you're probably going to hit, enter on the file 130 00:09:00,410 --> 00:09:01,130 name itself. 131 00:09:01,130 --> 00:09:05,090 First of all, you're going to see my file that the press enter. 132 00:09:05,660 --> 00:09:13,790 So my file that will be stored in file name, but there are high chances that the anther character that 133 00:09:13,790 --> 00:09:20,450 you press will be stored in the desired character and not the actual value that you want to read afterwards. 134 00:09:20,810 --> 00:09:23,060 Okay, so that's very important to bear this in mind. 135 00:09:23,300 --> 00:09:26,810 So if you're doing it like this, make sure you do it this way. 136 00:09:27,320 --> 00:09:27,680 OK. 137 00:09:28,190 --> 00:09:32,390 So, yeah, think you guys tested out on your own rights on file? 138 00:09:32,420 --> 00:09:36,620 Make sure that it works correctly if you have any questions suggestions. 139 00:09:37,400 --> 00:09:40,070 I don't know some dilemma that you want to ask and share. 140 00:09:40,520 --> 00:09:41,980 Feel free to do that. 141 00:09:42,050 --> 00:09:45,590 That's what we are doing here and that's why we are here. 142 00:09:45,830 --> 00:09:51,770 We are all here to study, to improve and to become better programmers and better developers. 143 00:09:53,230 --> 00:09:55,000 Oh, that's it. 144 00:09:55,240 --> 00:10:00,110 So my name is lad, this is Alpha Deck, and I will see you in the next videos. 145 00:10:00,140 --> 00:10:00,640 Bye bye. 13402

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.